I’m developing a serialization class that uses attributes on custom classes to decorate whether a property is a fixed length format or a delimited format. These two attributes should be mutually exclusive, meaning that the developer can either specify [FixedLength] or [Delimited] (with appropriate constructors) on a property, but not both. In order to reduce complexity and increase cleanliness, I don’t want to combine the attributes and set a flag based on the format type, e.g. [Formatted(Formatter=Formatting.Delimited)]. Is it possible to restrict these attributes to be mutually exclusive to one another at design time? I’m aware how I can check for this scenerio at run-time.
I’m developing a serialization class that uses attributes on custom classes to decorate whether
Share
You cannot do this in .NET. At most you can allow a single instance of the same attribute on a class, like in this example:
But this behavior cannot be extended to derived classes, i.e. if you do this it compiles:
The best I can think of is that you could write something to check that there are no types with both attributes applied and use the check as a post-build step.