gpt4 book ai didi

c# - 使用 Reflection.Emit 调用基类方法

转载 作者:行者123 更新时间:2023-11-30 16:55:16 27 4
gpt4 key购买 nike

我已经设置了我的代码来定义类型、设置父类型以及实现接口(interface)。我遇到的问题是,当我去创建类型时,它说它找不到接口(interface)的方法实现,即使这些方法是在父类型上实现的。据我所知,您需要为这种情况定义传递方法,这是我的代码:

    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);
}
}

当我尝试创建类型实例时出现此错误:“方法实现中的正文签名和声明不匹配。”我在这里缺少什么?

最佳答案

even though those methods are implemented on the parent type.

由于父类型已经实现了接口(interface),派生类型不需要显式传递(尽管您可能需要将接口(interface)的父类实现声明为虚拟)。

看看这个答案:https://stackoverflow.com/a/3621600/1698246

关于c# - 使用 Reflection.Emit 调用基类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29687257/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com