我可以从泛型类型T继承泛型类吗?像MyClass <t>:T </t>

时间:2011-04-27 10:01:18

标签: c# .net asp.net generics inheritance

我想要实现的是下面的内容,Visual Studio抱怨并且我不知道如何实现它:

public abstract class My_BaseControl<T> : T, IRightBasedUsability
        where T : System.Web.UI.WebControls.WebControl 
{

    // Some methods that I want to have for all 
    //the extended asp.net controls along my application
    //which implements IRightBasedUsability

}

2 个答案:

答案 0 :(得分:3)

在这种情况下,也许c#扩展方法可以解决您的问题。

@see http://msdn.microsoft.com/en-us/library/bb383977.aspx

Eric De Carufel撰写了一些有关扩展方法以及如何管理其范围的有趣文章: http://blog.decarufel.net/2009/02/extension-methods-series-managing-scope.html

答案 1 :(得分:1)

你根本不能从不存在的类型继承,不能;这就是你用你所展示的代码所做的事情。

您可以获得的最接近的是继承where T子句中的类。无论如何,效果不会有太大的不同。

相关问题