[英]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);
}
}
當我嘗試創建類型實例時,我收到此錯誤:“正文的簽名和方法實現中的聲明不匹配。” 我在這里錯過了什么?
即使這些方法是在父類型上實現的。
由於父類型已經在實現接口,因此派生類型不需要顯式傳遞(您可能需要將父接口的接口實現聲明為虛擬)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.