I need to call a generic method that takes a generic Func as one of its parameters, where the Type parameter is known only at runtime. This part of the code is an object mapper, which maps properties between a source and a target object. ViewModelBase is the root of classes that are considered "target" objects.
The method that I want to call (defined on 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();
// 1. How do I create the Func? All it does is return a private member.
// This is wrong because it uses a compile-time generic parameter.
Func<TTarget,IDictionary<String,object>> myFunc = (vm) => vm._propertyValues;
// 2. Ho do I call the Create method using reflection to specify the
// TTarget generic parameter at runtime?
var myMapper = ObjectMapperBuilder<TTarget>.Create(
sourceObject.GetType(),
MappingDirection.Bidirectional,
myFunc
);
// Do stuff with myMapper.
...
}
The purpose of this exercise is to be able to create the mapper in a method on the base class. The mapper must be created using the most derived type because I cache mappers according to source and target types, and different derived types need different mappers.
This might be a job for Expression trees and Activator, but I can't figure it out.
Part of the answer might be found in the answer to this question: