我想獲取現有屬性設置器的屬性定義。但是,即使我知道它存在,SetMethod總是顯示爲空。如何獲取屬性設置指令?
這是迄今爲止我嘗試:
public class Bar
{
private Foo _foo;
public virtual Foo Foo { get { return _foo; } { set { _foo = value; } }
}
var moduleDefinition = ModuleDefinition.ReadModule(assemblyFile);
var propertyTypeReference = new TypeReference(
typeof(Foo).Namespace,
typeof(Foo).Name,
moduleDefinition,
null
);
var propertyDefinition = new PropertyDefinition(
"Foo",
Mono.Cecil.PropertyAttributes.None,
propertyTypeReference
);
//always throws NullReferenceException because SetMethod is always null.
var instructions = propertyDefinition.SetMethod.Body.Instructions.ToList();
(我在微軟.NET CLR運行)。
如何使用Mono.Cecil獲取現有屬性的setter指令?