【发布时间】:2015-06-22 04:39:12
【问题描述】:
我正在为属性生成动态代理。
生成的代理派生自我们要代理的类型。
当代理需要访问其派生类型的(虚拟)属性时,OpCodes.Callvirt 无法使用 - 它会导致无限递归。因此我们需要调用OpCodes.Call。我注意到如果我有:
public class MyParent
{
protected string _name;
protected string _color;
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
public virtual string Color
{
get { return _color; }
set { _color = value; }
}
}
public class MyChild : MyParent
{
public override string Name {
get { return "42"; }
set { _name = value; }
}
}
当我在从MyChild 派生的代理对象上发出OpCodes.Call 以调用get_Color 时,它会被正确调用,即使从技术上讲,此方法并未在MyChild 上实现。
我打算编写一些代码,将类型层次结构向下遍历到 MyParent,其中可以找到 get_Color 实现,并为 OpCodes.Call 使用该类型方法,但似乎没有必要这样做:
var thisTypeMethod = property.GetGetMethod();
// I know that the next line technically is not correct because of non-virtual methods
// and also *new* overrides. Assume I'm doing it correctly, not by property.Name
// but by repeatedly calling MethodInfo.GetBaseDefinition()
var declaringTypeMethod = property.DeclaringType.GetProperty(property.Name).GetGetMethod();
然后
var proxyMethod = new DynamicMethod(thisTypeMethod.Name,thisTypeMethod.ReturnType, new Type[]{type},type,true);
var il = proxyMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Tailcall);
il.Emit(OpCodes.Call, thisTypeMethod);
il.Emit(OpCodes.Ret);
不使用 declaringTypeMethod 而使用 thisTypeMethod 是否安全?
【问题讨论】:
标签: c# .net reflection reflection.emit