我有一组这样定义的用户控件..
public class Control1: UserControl, Shop.Stock.IBlue
public class Control2: UserControl, Shop.Stock.IBlue
public class Control2: UserControl, Shop.Stock.IBlue
请注意,其中大约有 200 个,它们在实际项目中的命名更好。
我想在基于 UserControl 的对象上编写一个扩展方法并实现接口(interface) Shop.Stock.IBlue
我不希望扩展方法只是 UserControl
有没有一种方法可以在不添加新基类的情况下做到这一点?
最佳答案
如果我对问题的理解正确,你想定义一个仅适用于实现 IBlue
的 UserControls
的扩展方法。
public static void Foo<T>( this T obj )
where T : UserControl, IBlue
{
}
关于c# - 具有类型约束的扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9326769/