2 つの型パラメーターを必要とするメソッドを呼び出そうとしていますが、そのうちの 1 つは実行時まで不明です。
public static TTarget Map<TSource, TTarget>(TSource source, string key) {
var typeMapping = TypeMapper.mappings.FirstOrDefault(m => m.Key == key);
if (typeMapping.Value == null) return null;
Type type = Type.GetType(typeMapping.Value.ToString());
if (type == null) return null;
var method = typeof(Mapper).GetMethod("Map").MakeGenericMethod(typeof(TSource), type);
return method.Invoke(source); // throws exception
}
2 つの質問があります。
私はAutoMapperを使用しているので
Mapper.Map<TSource, TDestination>(TSource source)
、呼び出そうとしているメソッドですが、呼び出すときmethod.invoke(source)
に例外が発生しますメソッドの呼び出し (TSource) を解決できません。候補は、object invoke(object, object[])またはobject invoke(object, Reflection.BindingFlags, Reflection.Binder, object[], CultureInfo) です。
これが何を意味するかは理解できますが、引数 ofと not
Mapper.Map()
を渡して呼び出すにはどうすればよいですか?TSource source
object
TTarget
によって返されるオブジェクトの代わりにどのように返すことができmethod.Invoke()
ますか?