[英]Call generic method and to call method on return type
我该如何去只在运行时知道类型的通用方法然后又在返回的类型上调用方法呢? 我看了很多示例,但似乎无法正常工作。
到目前为止,这就是我所拥有的。
public interface IDataMapper<TEntity> where TEntity : IEntity
{
void Update(TEntity entity);
}
public IDataMapper<TEntity> GetMapper<TEntity>() where TEntity : IEntity
{
// Return something of type IDataMapper<TEntity>
}
foreach (IEntity entity in _dirtyObjects)
{
MethodInfo method = typeof(MapperFactory).GetMethod("GetMapper");
MethodInfo generic = method.MakeGenericMethod(entity.GetType());
generic.Invoke(_mapperFactory, null);
// I now want to call the Update() method
// I have tried to cast to IDataMapper<IEntity> which results in a null ref ex
}
感谢您的任何建议。
您必须继续思考:
object dataMapper = generic.Invoke(_mapperFactory, null);
method = dataMapper.GetType().GetMethod("Update");
method.Invoke(dataMapper, new object[] {entity});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.