繁体   English   中英

向泛型类添加扩展方法

[英]Adding an Extension Method to a Generic class

如何创建我的Container类的扩展? 无需测试数据库类型。

public abstract class DBAccess : IDBAccess, IDisposable
{
}

public class DB1 : DBAccess
{
}

public class DB2 : DBAccess
{
}

public partial class TAContainer<T> : TableAdapterModel<T, DTContainer>, ITAContainer
    where T : DBAccess, new()
{
}

现在我的扩展程序想要一些东西

public static int ExecuteByExtension(this ITAContainer tAContainer, IDTContainer dataTable)
{
    var ta = (tAContainer as TableAdapter<DB1>);


}

所有这一切。 但我想将变量ta设置为抽象类“ DBAccess”。 无法做到这一点,也无法使用接口。

如何为铸造使用单个类型。 并且不测试DB1或DB2

结果应该这样称呼:

var dataTable = new DTContainer() as IDTContainer;
var container = new TAContainer<DB1>() as ITAContainer;
int result = container.ExecuteByExtension(datatable);

您能否将通用参数添加到扩展方法中,如下所示:

public static int ExecuteByExtension<T>(
     this TAContainer<T> tAContainer, IDTContainer dataTable) where T : DBAccess, new()
{
    var ta = (tAContainer as TableAdapter<T>);
    // ...
    return 0;
}

暂无
暂无

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

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