1

i have method that its signature looks like

public object LoadCache(string cacheName,Type returnType)

and from this method another method is called which signature like this

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

is there any way to send returnType parameter as T in the method?

i believe that there is no way to do that! but there must be a way around it!

MuhanadY
  • 752
  • 1
  • 12
  • 40
  • 1
    Sure its possible. http://msdn.microsoft.com/en-us/library/system.type.makegenerictype.aspx – asawyer Oct 02 '13 at 13:16
  • 2
    It's possible, but you will have to use reflection. – Jon Oct 02 '13 at 13:17
  • 5
    Any reason to not make `LoadCache` generic? – Alessandro D'Andria Oct 02 '13 at 13:18
  • the point here that we are using wcf and its impossible to pass a generic parameter throw the service. there for we must have a we to pass there type of the cache to load it from the provider. and it takes .and we do not know what T code be until run time. therefor am trying to pass the returntype as parameter and convert it to T :) by the way i do not think that Type.MakeGenericType might help. – MuhanadY Oct 02 '13 at 13:27
  • 2
    Problem answered at: http://stackoverflow.com/questions/2107845/generics-in-c-using-type-of-a-variable-as-parameter – borkovski Oct 02 '13 at 13:30
  • 1
    If you can't make LoadCache generic, since it returns `object` anyway, can you just use `GetCache`? – mao47 Oct 02 '13 at 14:18
  • @mao47 actually you might be right, but the point is that T is a known type which will be set at run time from the client side! wcf makes it hard some times :) – MuhanadY Oct 03 '13 at 05:26

1 Answers1

0

i found a similar Solution here which i did change it as needed. so reflection is the hero.

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