C# 无法从';系统类型';自定义接口
我定义了自己的C# 无法从';系统类型';自定义接口,c#,generics,types,constraints,typeconverter,C#,Generics,Types,Constraints,Typeconverter,我定义了自己的IExportable接口,并将其用作 public static A SomeAction<B>(IList<T> data) where T : IExportable { var tType = typeof(T); IList<B> BLists = SomeMethod(tType); //... } 但当我运行应用程序时,会出现以下错误: SomeMethod(IExportable)的最佳重载方法匹配具有
IExportable
接口,并将其用作
public static A SomeAction<B>(IList<T> data) where T : IExportable
{
var tType = typeof(T);
IList<B> BLists = SomeMethod(tType);
//...
}
但当我运行应用程序时,会出现以下错误:
SomeMethod(IExportable)的最佳重载方法匹配具有一些无效参数
无法从“System.Type”转换为“IFileExport”我的错误在哪里?
typeof(T)
返回一个对象,该对象包含由T
表示的类的元信息SomeMethod
正在查找扩展IExportable
的对象,因此您可能希望创建一个扩展IExportable
的T
对象。您有几个选项可以执行此操作。最直接的选择可能是在泛型参数上添加new
约束,并使用T
的默认构造函数
//Notice that I've added the generic paramters A and T. It looks like you may
//have missed adding those parameters or you have specified too many types.
public static A SomeAction<A, B, T>(IList<T> data) where T : IExportable, new()
{
T tType = new T();
IList<B> BLists = SomeMethod(tType);
//...
}
typeof(T)
返回一个对象,该对象包含由T
表示的类的元信息SomeMethod
正在查找扩展IExportable
的对象,因此您可能希望创建一个扩展IExportable
的T
对象。您有几个选项可以执行此操作。最直接的选择可能是在泛型参数上添加new
约束,并使用T
的默认构造函数
//Notice that I've added the generic paramters A and T. It looks like you may
//have missed adding those parameters or you have specified too many types.
public static A SomeAction<A, B, T>(IList<T> data) where T : IExportable, new()
{
T tType = new T();
IList<B> BLists = SomeMethod(tType);
//...
}
typeof(T)返回System.Type的实例,但您的方法采用IExportable。typeof(T)返回System.Type的实例,但您的方法采用IExportable。
public static A SomeAction<B>(IList<T> data) where T : IExportable
{
//Notice what typeof returns.
System.Type tType = typeof(T);
IList<B> BLists = SomeMethod(tType);
//...
}