jeudi 15 janvier 2015

How to call a method with struct constraint to unknown struct

Question is simple: I'm using a reflection to get a value. Then if it's a struct, I'm calling a method FooStruct, else FooClass:



Type type = x.GetType();
foreach (var fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
var val = fieldInfo.GetValue(value);
object obj = type.IsValueType ? val.FooStruct() : val.FooClass();
fieldInfo.SetValue(x, obj);
}


problem is that FooStruct has a constraint:



public static T FooStruct<T>(this T value) where T : struct
{
//...
}


so question is: is it possible to call this method without using reflection?






Aucun commentaire:

Enregistrer un commentaire