I need to call a generic method that accepts a generic Func as one of its parameters, where the Type parameter is known only at runtime. This piece of code is a mapper object that maps properties between the source and the target. ViewModelBase is the root of the classes that are considered targets.
The method I want to call (defined in ObjectMapperBuilder) has this signature:
public static ObjectMapperBuilder<TTarget> Create(
Type sourceType,
MappingDirection direction,
Func<TTarget, IDictionary<String, object>> getDictionaryFromTarget = null
);
In my base class, I want to call the above method, but use the most derived type as my type parameter:
public ViewModelBase {
private ConcurrentDictionary<string, object> _propertyValues;
public ViewModelBase (object sourceObject) {
Type tTarget = this.GetType();
Func<TTarget,IDictionary<String,object>> myFunc = (vm) => vm._propertyValues;
var myMapper = ObjectMapperBuilder<TTarget>.Create(
sourceObject.GetType(),
MappingDirection.Bidirectional,
myFunc
);
...
}
- Mapper . Mapper , mappers , .
, .
:
Func <T>