开发者

C# using generic method with Type [duplicate]

This question already has answers here: How do I use reflection to call a generic method? (8 answers) Closed 8 years ago.

I'm using .Net framework 2.0 to try and do the following:

I've an external service which returns a list of int. In turn I use each int to find a corresponding Type which has an Attribute with a property, key; the value of that property matches the search parameter.

Using the Type t I'd like to call a generic method, but I'm unable to do this. As I will only know the Type at runtime, I suspect I may have to use reflection to invoke the generic method GetResultsForType - is this the correct way to go about this?

[MyAttribute(key1 = 1)]
class A{
    //some properties
}

[MyAttribute(key1 = 2)]
class B{
    //some properties
}

//and so on (for hundreds of classes).  The key is unique for every class.

public class Foo{
    public void DoSomething(){
         IList<int> keys = QuerySomeExternalService();

         Assembly asm = LoadAssemblyFromSomewhere();
         Type[] types = asm.GetTypes();
         foreach(int key in keys){
             Type t = SearchTypesForAttributeWithMatchingKey(types, key);  //I omitted caching of all the keys and Types into a Dictionary on first iteration for brevity.
             IList<t> results = GetResultsForType<t>(); //won't work!
             //do something with the results
         }
    }

    Type SearchTypesForAttributeWithMatchingKey(Type[] types, int key){
        foreach(Type t in types开发者_运维知识库){
            object[] attributes = t.GetCustomAttributes(typeof(MyAttribute),false);
            MyAttribute myAtt = attributes[0] as MyAttribute;
            if(myAtt.Key == key) return t;
        }
    }

    IList<T> GetResultsForType<T>(){
        IList<T> results = new List<T>();
        bool querySuccess = true;
        while(querySuccess){
            T result;
            querySuccess = QueryExternalService<T>(out result);
            results.Add(result);
        }
        return results;
    }
}


Yes you have to use reflection to call a generic method using a System.Type rather than a generic parameterization. You can use the method on methodInfo MakeGenericMethod(params Type[]) after finding the generic method.

If you know the method is called often but with a small number of types, you can cache delegates to invoke the right version.

Something like::

typeof(Foo).GetMethod("YourMethod").MakeGenericMethod(type).Invoke(new[]{parameters});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜