About Typed parameters
-
Hi everybody, I have this abstract method:
public static T DecodeData<T>(byte[] data, string encRules)
But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.
public static Type GetDownloadType (int id) { switch (id) { case DOWNDIRECT: return typeof(DownDirect); case DOWNOTHER: return typeof(DownOther); case DOWNHEHE: return typeof(DownHehe); default: return null; } }
But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced
-
Hi everybody, I have this abstract method:
public static T DecodeData<T>(byte[] data, string encRules)
But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.
public static Type GetDownloadType (int id) { switch (id) { case DOWNDIRECT: return typeof(DownDirect); case DOWNOTHER: return typeof(DownOther); case DOWNHEHE: return typeof(DownHehe); default: return null; } }
But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced
T needs to be resolvable at compile time i.e before runtime when you have access to the type via
typeof
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hi everybody, I have this abstract method:
public static T DecodeData<T>(byte[] data, string encRules)
But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.
public static Type GetDownloadType (int id) { switch (id) { case DOWNDIRECT: return typeof(DownDirect); case DOWNOTHER: return typeof(DownOther); case DOWNHEHE: return typeof(DownHehe); default: return null; } }
But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced
-
Hi everybody, I have this abstract method:
public static T DecodeData<T>(byte[] data, string encRules)
But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.
public static Type GetDownloadType (int id) { switch (id) { case DOWNDIRECT: return typeof(DownDirect); case DOWNOTHER: return typeof(DownOther); case DOWNHEHE: return typeof(DownHehe); default: return null; } }
But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced
If you really need to invoke a generic method you could use something like this:
public static class Test
{
public static T GenericDecodeData<T>(byte[] data, string encRules)
{
// ...
return default(T);
}public static Type GetDownloadType(int id) { switch (id) { case DOWNDIRECT: return typeof(DownDirect); case DOWNOTHER: return typeof(DownOther); case DOWNHEHE: return typeof(DownHehe); default: return null; } } public static object DecodeData(Type type, byte\[\] data, string encRules) { MethodInfo methodInfo = typeof(Test).GetMethod("GenericDecodeData").MakeGenericMethod( new Type\[\] { type }); return methodInfo.Invoke(null, new object\[\] { data, encRules }); }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
If you really need to invoke a generic method you could use something like this:
public static class Test
{
public static T GenericDecodeData<T>(byte[] data, string encRules)
{
// ...
return default(T);
}public static Type GetDownloadType(int id) { switch (id) { case DOWNDIRECT: return typeof(DownDirect); case DOWNOTHER: return typeof(DownOther); case DOWNHEHE: return typeof(DownHehe); default: return null; } } public static object DecodeData(Type type, byte\[\] data, string encRules) { MethodInfo methodInfo = typeof(Test).GetMethod("GenericDecodeData").MakeGenericMethod( new Type\[\] { type }); return methodInfo.Invoke(null, new object\[\] { data, encRules }); }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)