Should properties be emitted by using a
MethodBuilder
or using aPropertyBuilder
?
Both. If you have a read-write property X
, then in CIL it's represented as a a get method (usually called get_X
), a set method (usually called set_X
) and a properly called X
that points to the two methods.
So, to create a property using Reflection.Emit, you should:
- Use
MethodBuilder
to create the get method (probably reading from some field). - Use another
MethodBuilder
to create the set method (probably writing to the same field). - Use
PropertyBuilder
to create the property, setting itsName
and callingSetGetMethod()
andSetSetMethod()
.
If you want to create a read-only property, just skip step 2 and don't call GetSetMethod()
.