Question

I defined my own IExportable interface, and use it as

public static A SomeAction<B>(IList<T> data) where T : IExportable
{
    var tType = typeof(T);
    IList<B> BLists = SomeMethod(tType);
    //...
} 

that SomeMethod is:

List<B> SomeMethod(IExportable exportData)
{
   // do somethings
}

but when i run my application get this error:

The best overloaded method match for SomeMethod(IExportable)has some invalid arguments cannot convert from 'System.Type' to 'IFileExport'
where is my mistake?

Était-ce utile?

La solution

typeof(T) returns a Type object that has meta information about the class that is represented by T. SomeMethod is looking for an object that extends IExportable, so you probably want to create an object that is a T which extends IExportable. You have a few options to do this. The most straight forward option may be to add the new constraint on your generic paramter and use T's default constructor.

//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);
    //...
} 

I've explicitly stated the type of tType to better illustrate what's going on in your code:

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);
    //...
}  
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top