Question

I would like to ask what is the purpose of PropertyAttribute.HasDefault enum value in context of defining property using System.Reflection.Emit.TypeBuilder.DefineProperty method? When one should use PropertyAttributes.HasDefault and when PropertyAttributes.None and what does it change?

As far as I can see it's related to SetConstant method of PropertyBuilder but it doesn't really change default value of property as far as I was able to test, so what does SetConstant really do?

(I don't intent to change property's default value - just trying to understand what these things do)

Was it helpful?

Solution

SetConstant is a wrapper around TypeBuilder.SetConstantValue(), which is used to associate compile-time constants with certain IL metadata, most commonly parameters (with default values) and const fields.

Its usefulness for properties seems rather dubious; it does not affect what value is actually returned by the property's get accessor. It would seem the default value set via SetConstant() can only be inspected in the metadata or via reflection. It might be useful if you want to allow someone to determine the default value returned by a property using reflection or IL analysis only, but since the get accessor is not required to actually return that value, ever, it should be taken with a grain of salt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top