使用 Reflection.Emit 调用基础 Class 方法
Calling Base Class Method using Reflection.Emit
我已经设置我的代码来定义类型、设置父类型以及实现接口。我遇到的问题是,当我去创建类型时,它说它找不到接口的方法实现,即使这些方法是在父类型上实现的。据我所知,您需要为这种情况定义传递方法,这是我的代码:
public interface IService<TDto, TId>
{
Task<TId> CreateAsync(TDto entity);
Task<bool> DeleteAsync(TId id);
Task<bool> UpdateAsync(TDto entity);
Task<List<TDto>> GetAllAsync();
Task<TDto> GetAsync(TId id);
TDto Get(TId id);
Task<IEnumerable<TDto>> GetAllPagedAsync(Int32 page, Int32 take);
Task<Int32> GetCountAsync();
Task<object> GetForIdsAsync(TId[] ids, Int32? page = null, Int32? pageSize = null);
object GetForIds(TId[] ids, Int32? page = null, Int32? pageSize = null);
Task<bool> DeleteForIdsAsync(TId[] ids);
Task<List<TDto>> CloneEntitiesAsync(List<TDto> dtos);
Task<IList> GetForTypesAndIdsAsync(List<string> types, List<object[]> typeIds);
Task<object> GetByConditionsAsync(Query query);
Task<object> ExceptIdsAsync(TId[] ids, Int32? page = null, Int32? pageSize = null);
bool Merge(TDto dto);
}
public Type[] CreateServiceType(Type dtoType, Type entityType, ModuleBuilder moduleBuilder)
{
string namespaceName = string.Format("Services.{0}", ProfileNamePlural);
TypeBuilder iserviceTypeBuilder =
moduleBuilder.DefineType(
string.Format("{0}.I{1}Service", namespaceName, ClassName),
TypeAttributes.Interface | TypeAttributes.Abstract);
var baseIServiceType = typeof (IService<,>).MakeGenericType(dtoType, entityType);
iserviceTypeBuilder.AddInterfaceImplementation(baseIServiceType);
Type iserviceType = iserviceTypeBuilder.CreateType();
var baseType = typeof(AService<,,>).MakeGenericType(dtoType, entityType, typeof(Guid));
string serviceClassName = string.Format("{0}.{1}Service", namespaceName, ClassName);
TypeBuilder serviceTypeBuilder =
moduleBuilder.DefineType(serviceClassName, TypeAttributes.Public);
serviceTypeBuilder.SetParent(baseType);
serviceTypeBuilder
.AddInterfaceImplementation(iserviceType);
var repositoryType = typeof(IRepository<>).MakeGenericType(entityType);
ConstructorBuilder ctorBuilder =
serviceTypeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
new[] { repositoryType });
var baseConstructor =
baseType.GetConstructor(
BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance,
null,
new[] { repositoryType },
null);
var ilGenerator = ctorBuilder.GetILGenerator();
// Generate constructor code
ilGenerator.Emit(OpCodes.Ldarg_0); // push "this" onto stack.
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Call, baseConstructor);
ilGenerator.Emit(OpCodes.Ret);
DefinePassThroughs(ref serviceTypeBuilder, baseType, baseIServiceType);
return new[] { serviceTypeBuilder.CreateType(), iserviceType };
}
private void DefinePassThroughs(ref TypeBuilder typeBuilder, Type baseType, Type iServiceType)
{
var virtualMethods = iServiceType.GetMethods();
foreach (var imethod in virtualMethods)
{
var method = baseType.GetMethod(imethod.Name);
var paramTypes = method.GetParameters().Select(x => x.ParameterType).ToArray();
var passThroughMethod =
typeBuilder.DefineMethod(
method.Name,
MethodAttributes.Public,
CallingConventions.Standard,
method.ReturnType,
paramTypes);
var il = passThroughMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
for (var i = 0; i < paramTypes.Length; i++)
{
il.Emit(OpCodes.Ldarg, i + 1);
}
il.EmitCall(OpCodes.Callvirt, method, null);
il.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(passThroughMethod, imethod);
}
}
当我尝试创建类型实例时出现此错误:"Signature of the body and declaration in a method implementation do not match."我在这里遗漏了什么?
even though those methods are implemented on the parent type.
由于父类型已经实现了接口,派生类型不需要显式传递(尽管您可能需要将接口的父类实现声明为虚拟)。
看看那个答案:
我已经设置我的代码来定义类型、设置父类型以及实现接口。我遇到的问题是,当我去创建类型时,它说它找不到接口的方法实现,即使这些方法是在父类型上实现的。据我所知,您需要为这种情况定义传递方法,这是我的代码:
public interface IService<TDto, TId>
{
Task<TId> CreateAsync(TDto entity);
Task<bool> DeleteAsync(TId id);
Task<bool> UpdateAsync(TDto entity);
Task<List<TDto>> GetAllAsync();
Task<TDto> GetAsync(TId id);
TDto Get(TId id);
Task<IEnumerable<TDto>> GetAllPagedAsync(Int32 page, Int32 take);
Task<Int32> GetCountAsync();
Task<object> GetForIdsAsync(TId[] ids, Int32? page = null, Int32? pageSize = null);
object GetForIds(TId[] ids, Int32? page = null, Int32? pageSize = null);
Task<bool> DeleteForIdsAsync(TId[] ids);
Task<List<TDto>> CloneEntitiesAsync(List<TDto> dtos);
Task<IList> GetForTypesAndIdsAsync(List<string> types, List<object[]> typeIds);
Task<object> GetByConditionsAsync(Query query);
Task<object> ExceptIdsAsync(TId[] ids, Int32? page = null, Int32? pageSize = null);
bool Merge(TDto dto);
}
public Type[] CreateServiceType(Type dtoType, Type entityType, ModuleBuilder moduleBuilder)
{
string namespaceName = string.Format("Services.{0}", ProfileNamePlural);
TypeBuilder iserviceTypeBuilder =
moduleBuilder.DefineType(
string.Format("{0}.I{1}Service", namespaceName, ClassName),
TypeAttributes.Interface | TypeAttributes.Abstract);
var baseIServiceType = typeof (IService<,>).MakeGenericType(dtoType, entityType);
iserviceTypeBuilder.AddInterfaceImplementation(baseIServiceType);
Type iserviceType = iserviceTypeBuilder.CreateType();
var baseType = typeof(AService<,,>).MakeGenericType(dtoType, entityType, typeof(Guid));
string serviceClassName = string.Format("{0}.{1}Service", namespaceName, ClassName);
TypeBuilder serviceTypeBuilder =
moduleBuilder.DefineType(serviceClassName, TypeAttributes.Public);
serviceTypeBuilder.SetParent(baseType);
serviceTypeBuilder
.AddInterfaceImplementation(iserviceType);
var repositoryType = typeof(IRepository<>).MakeGenericType(entityType);
ConstructorBuilder ctorBuilder =
serviceTypeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
new[] { repositoryType });
var baseConstructor =
baseType.GetConstructor(
BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance,
null,
new[] { repositoryType },
null);
var ilGenerator = ctorBuilder.GetILGenerator();
// Generate constructor code
ilGenerator.Emit(OpCodes.Ldarg_0); // push "this" onto stack.
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Call, baseConstructor);
ilGenerator.Emit(OpCodes.Ret);
DefinePassThroughs(ref serviceTypeBuilder, baseType, baseIServiceType);
return new[] { serviceTypeBuilder.CreateType(), iserviceType };
}
private void DefinePassThroughs(ref TypeBuilder typeBuilder, Type baseType, Type iServiceType)
{
var virtualMethods = iServiceType.GetMethods();
foreach (var imethod in virtualMethods)
{
var method = baseType.GetMethod(imethod.Name);
var paramTypes = method.GetParameters().Select(x => x.ParameterType).ToArray();
var passThroughMethod =
typeBuilder.DefineMethod(
method.Name,
MethodAttributes.Public,
CallingConventions.Standard,
method.ReturnType,
paramTypes);
var il = passThroughMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
for (var i = 0; i < paramTypes.Length; i++)
{
il.Emit(OpCodes.Ldarg, i + 1);
}
il.EmitCall(OpCodes.Callvirt, method, null);
il.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(passThroughMethod, imethod);
}
}
当我尝试创建类型实例时出现此错误:"Signature of the body and declaration in a method implementation do not match."我在这里遗漏了什么?
even though those methods are implemented on the parent type.
由于父类型已经实现了接口,派生类型不需要显式传递(尽管您可能需要将接口的父类实现声明为虚拟)。
看看那个答案: