Craig Bovis
Craig Bovis

Reputation: 2811

Requiring Properties of Custom Attributes in .NET

I have a bunch of custom attributes in my .NET application and I would like to require the user fills out certain properties within the attribute at runtime. Is this possible?

Ideally I'd like Visual Studio to produce an error when they try to build which states that since they used a particular attribute they must complete X, Y, Z properties of that attribute.

Upvotes: 0

Views: 125

Answers (1)

JaredPar
JaredPar

Reputation: 754575

If you want to force a property to be set then only define constructors on the attribute which take fields corresponding to those properties. This will force the values to be passed at usage time and you can assign them to the property inside the constructor.

Upvotes: 7

Related Questions