2013-10-02 43 views
1

我有方法,它的签名看起来像泛型类型转换益智

public object LoadCache(string cacheName,Type returnType) 

,并从该方法的另一种方法被称为它的签名是这样

BBCacheProvider.BbCacheProvider.GetCache<T>(string cacheName); 

有没有办法送返回类型参数作为T在方法中?

我相信没有办法做到这一点!但一定有办法解决它!

+1

当然有可能。 http://msdn.microsoft.com/en-us/library/system.type.makegenerictype.aspx – asawyer

+2

这是可能的,但你将不得不使用反射。 – Jon

+5

任何不能使LoadCache通用的原因? –

回答

0

我发现了一个类似的Solution在这里,我根据需要进行了更改。所以反思就是英雄。

public void ClientCacheLoadRerquest(Type tType, string key) 
    { 
     MethodInfo method = this.GetType().GetMethod("GetCache"); 
     MethodInfo closedMethod = method.MakeGenericMethod(tType); 
     closedMethod.Invoke(this, new object[] { key }); 
    }