I want to dynamically fit type to argument in my method yet before compiling, i mean:
public class Foo
{
public int Prop1;
public string Prop2;
}
public class Program
{
public static void Main(string[] args)
{
Foo foo = MakeMethod(x => x.Prop1, 5); //good, Prop1 is int type, 5 isn't underlined in red in VS
Foo foo = MakeMethod(x => x.Prop2, 10); //bad, Prop2 is string, not int, VS underlines 10 in red, and I want this behaviour
}
private static Foo MakeMethod(Func<Foo, object> prop, object val)
{
Foo result;
//do some stuff
return result;
}
}
I know I should do something with object type, but i dunno what. I could simply check if second generic prop argument is the type of val, otherwise do return in my method, but that's not satisfying me. I don't want to use dynamic type either because I get exception after running my program, not at the code writing stage.
Aucun commentaire:
Enregistrer un commentaire