显式方法的覆盖覆盖隐藏属性

时间:2012-08-07 11:46:07

标签: c# reflection.emit

有一个有一个属性的类:

public class BaseClass
{
  public virtual string Property1 { get; set; }
}

现在我使用一些方法覆盖创建一个派生类型:

[Test]
public void name ()
{
  var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName ("Test"), AssemblyBuilderAccess.RunAndSave);
  var moduleBuilder = assemblyBuilder.DefineDynamicModule ("Test.dll");
  var derivedBuilder = moduleBuilder.DefineType ("DerivedClass", TypeAttributes.Public, typeof (BaseClass));

  const MethodAttributes methodAttributes = MethodAttributes.Private | MethodAttributes.Virtual | MethodAttributes.SpecialName | MethodAttributes.NewSlot;

  var getterOverride = derivedBuilder.DefineMethod (
      "get_Property1",
      methodAttributes,
      typeof (string),
      Type.EmptyTypes);
  var getterILGenerator = getterOverride.GetILGenerator();
  getterILGenerator.Emit (OpCodes.Ldnull);
  getterILGenerator.Emit (OpCodes.Ret);
  derivedBuilder.DefineMethodOverride (getterOverride, typeof (BaseClass).GetMethod ("get_Property1"));

  var setterOverride = derivedBuilder.DefineMethod (
      "set_Property1",
      methodAttributes,
      typeof (void),
      new[] { typeof (string) });
  var setterILGenerator = setterOverride.GetILGenerator ();
  setterILGenerator.Emit (OpCodes.Ret);
  derivedBuilder.DefineMethodOverride (setterOverride, typeof (BaseClass).GetMethod ("set_Property1"));

  var derivedType = derivedBuilder.CreateType();
  var props = derivedType.GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

  assemblyBuilder.Save ("Test.dll");

  Assert.That (props, Has.Length.EqualTo (1));
}

生成的类型不再包含该属性。奇怪的是,如果您为至少一个方法定义将methodAttributes更改为MethodAttributes.Public,则该属性会再次出现。

好像是个错误?

编辑: peverify不会出错。

编辑(Fabian Schmied的重要评论)

ECMA-335分区II,10.3.3:“如果类型通过MethodImpl覆盖继承的方法,它可以扩大或缩小该方法的可访问性。”

1 个答案:

答案 0 :(得分:4)

由于specs(ECMA-335,II.10.3.3)明确允许通过MethodImpl缩小被覆盖的方法的可见性(“显式覆盖”,由Reflection.Emit通过TypeBuilder.DefineMethodOverride表示) ,我相信这确实是Reflection实现中的一个错误,应该通过Microsoft Connect报告。