How do I support generic expression in C# 2.0 -
i have code written c# 4.0+, need compile c# 2.0 compiler.
the following snippets don't how write, in order support same functionality:
public sealed class myclass { private static readonly genericstaticmethod _deserializehelper = new genericstaticmethod(() => deserializehelper<object>(null, null, null)); //... } public sealed class genericstaticmethod { private readonly methodinfo methodtocall; public genericstaticmethod(expression<action> methodcall) { var callexpression = (methodcallexpression)methodcall.body; methodtocall = callexpression.method.getgenericmethoddefinition(); } public object invoke(type[] genericarguments, params object[] arguments) { try { return methodtocall .makegenericmethod(genericarguments) .invoke(null, arguments); } catch (targetinvocationexception ex) { throw ex.unwrap(); } } }
the expression cannot written way is:
() => deserializehelper<object>(null, null, null)
a straight substitution delegate doesn't work without modifications of genericstaticmethod()
:
delegate() { return deserializehelper<object>(null, null, null); }
rewriting genericstaticmethod
acceptable, don't know how to.
you plain cannot use expression trees in case, compiler support in later versions. don't need to, if reason use methodinfo
in strongly-typed fashion.
public sealed class myclass { private static readonly genericstaticmethod _deserializehelper = new genericstaticmethod(new action<object, object, object>(deserializehelper<object>)); } public sealed class genericstaticmethod { private readonly methodinfo methodtocall; public genericstaticmethod(delegate methodcall) { methodtocall = methodcall.method.getgenericmethoddefinition(); } }
you'll need change action<object, object, object>
delegate type matches signature of deserializehelper<object>
.
Comments
Post a Comment