In C#, we can create a private property by doing:
private string Name { get; set; }
However, suppose we are creating a property using Reflection.Emit.PropertyBuilder.
The following code will create a public property (getter and setter methods not yet defined):
PropertyBuilder prop = typeBuilder.DefineProperty("Name", PropertyAttributes.None, CallingConvention.HasThis, typeof(string), Type.EmptyTypes);
How will I define this same property but with private visibility?
Reflection.Emit.FieldBuilder can be specified a FieldAttributes.Private
, but PropertyBuilder
does not seem to offer something similar.
Is this possible with Reflection.Emit?
When building properties you can set private get / set properties as shown below. You cannot set the Property itself as private.
From the documentation, the relevant code is shown below.