c# - Create delegate with types known at runtime -


how can create delegate types known @ run time ?

i following :

type type1 = someobject.gettype(); type type2 = someotherobject.gettype();     var getter = (func<type1, type2>)delegate.createdelegate(     typeof(func<,>).makegenerictype(type1, type2), somemethodinfo); 

how can achieve similar ?

i suspect want expression.getfunctype simpler way of doing typeof(...).makegenerictype operation.

var delegatetype = expression.getfunctype(type1, type2); delegate getter = delegate.createdelegate(delegatetype, somemethodinfo); 

you can't have compile-time type getter that's more specific delegate though1, because don't know type @ compile-time. potentially use dynamic though, make easier invoke delegate:

dynamic getter = ...; var result = getter(input); 

1 noted in comments, could cast multicastdelegate, wouldn't buy anything.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -