简体   繁体   English

我可以从泛型类型 T 继承泛型 class 吗? 喜欢我的班级<t> : T</t>

[英]Can I inherit a generic class from a generic type T? Like MyClass<T> : T

What I want to achieve is something like this below, which Visual Studio complains about and I don't know how to achieve it:我想要实现的是下面这样的东西,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

}

Perhaps c# extension methods are in this case a way to solve your problem.在这种情况下,也许 c# 扩展方法是解决您问题的一种方法。

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

Eric De Carufel wrote some interesting articles about extension methods and how to manage their scope: http://blog.decarufel.net/2009/02/extension-methods-series-managing-scope.html Eric De Carufel 写了一些关于扩展方法以及如何管理他们的 scope 的有趣文章: http://blog.decarufel.net/2009/02/extension-methods-series-managing-scope.ZFC256FDC70D2FCCAD9

You can't inherit from a non-existent type at all, no;您根本不能从不存在的类型继承,不; that's what you would be doing with the code you've shown.这就是您将使用您显示的代码所做的事情。

The closest you can get would be to inherit from the class in your where T clause.您可以获得的最接近的方法是从where T子句中的 class 继承。 The effect would not be much different, anyway.无论如何,效果不会有太大不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM