我正在尝试在 ILGenerator 中编写一些有助于延迟加载的代码。我遇到的问题是在使用 TypeBuilder 构建的类的私有字段上找到的加载方法。
我想在 IL 中完成的是以下
class Proxy
: BaseType
{
private DbContextAccessor _accessor;
private Status _Status;
public override Status Status
{
get
{
if (_Status.IsNull())
{
PropertyInfo info = GetType().GetProperty("Status");
_Status = _accessor.Load<BaseType, Status>(this, info);
}
return _Status;
}
set
{
_Status = value;
}
}
}
以下是我目前写的ILGenerator代码
private void BuildOverriddenProperty(string propertyName, Type propertyType)
{
FieldBuilder propertyField = typeBuilder.DefineField($"_{propertyName}", propertyType, FieldAttributes.Private);
PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(
propertyName,
PropertyAttributes.None,
propertyType,
Type.EmptyTypes);
MethodAttributes methodAttributesForGetAndSet = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual ;
MethodBuilder
getMethod = typeBuilder.DefineMethod($"get_{propertyName}", methodAttributesForGetAndSet, propertyType, Type.EmptyTypes),
setMethod = typeBuilder.DefineMethod($"set_{propertyName}", methodAttributesForGetAndSet, null, new Type[] { propertyType });
ILGenerator
iLGetGenerator = getMethod.GetILGenerator(),
iLSetGenerator = setMethod.GetILGenerator();
Type internalExt = typeof(InternalExtensions);
MethodInfo
load = fieldDbContextAccessor.FieldType.GetMethods()
.Where(method =>
method.Name == (propertyType.IsClass ? "Load" : "LoadCollection"))
.Single().MakeGenericMethod(baseType),
isNull = internalExt.GetMethod("IsNull", BindingFlags.Public | BindingFlags.Static , null, new[] { typeof(object) }, null);
Label fieldIsNotNull = iLGetGenerator.DefineLabel();
LocalBuilder
propertyInfo = iLGetGenerator.DeclareLocal(typeof(PropertyInfo));
iLGetGenerator.Emit(OpCodes.Ldarg_0); // this
iLGetGenerator.Emit(OpCodes.Ldfld, propertyField); // propertyField
iLGetGenerator.EmitCall(OpCodes.Call, isNull, null); // use the static extension method IsNull
iLGetGenerator.Emit(OpCodes.Brfalse_S, fieldIsNotNull); // value is not null
//{
iLGetGenerator.Emit(OpCodes.Ldarg_0); // this
iLGetGenerator.EmitCall(OpCodes.Call, typeof(object).GetMethod("GetType"), null); // call GetType method
iLGetGenerator.Emit(OpCodes.Ldstr, propertyName); // push new string of propertyName
iLGetGenerator.EmitCall(OpCodes.Call, typeof(Type).GetMethod("GetProperty", new[] { typeof(string) }), null); // call GetProperty with the propertyName as the parameter
iLGetGenerator.Emit(OpCodes.Stloc, propertyInfo); // store PropertyInfo object in the local variable propertyInfo
// -> this is the problem area that results in invalid program code
iLGetGenerator.Emit(OpCodes.Ldarg_0); // this
iLGetGenerator.Emit(OpCodes.Ldfld, fieldDbContextAccessor); // field variable _accessor
iLGetGenerator.Emit(OpCodes.Ldarg_0); // this ptr as the first parameter // <- my hunch is this is the problem but uncertain
iLGetGenerator.Emit(OpCodes.Ldloc, propertyInfo); // local variable propertyInfo as the second parameter
iLGetGenerator.EmitCall(OpCodes.Call, load, null); // call the Load or LoadCollection on the DBContextAccessor object
iLGetGenerator.Emit(OpCodes.Stfld, propertyField); // store the return in the propertyField
// -> end
//}
iLGetGenerator.MarkLabel(fieldIsNotNull); // jump here when propertyField is not null
iLGetGenerator.Emit(OpCodes.Ldarg_0); // this
iLGetGenerator.Emit(OpCodes.Ldfld, propertyField); // propertyField
iLGetGenerator.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(getMethod, baseType.GetProperty(propertyName).GetMethod);
iLSetGenerator.Emit(OpCodes.Ldarg_0);
iLSetGenerator.Emit(OpCodes.Ldarg_1);
iLSetGenerator.Emit(OpCodes.Stfld, propertyField);
iLSetGenerator.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(setMethod, baseType.GetProperty(propertyName).SetMethod);
}