Custom compile-time errors and warnings

  • Thread starter Thread starter Glen
  • Start date Start date
G

Glen

I'm working on a custom assembly and I'm trying to figure out the best
approach to handling known constraints within the assembly, once
compiled, to alert the developer at compile time of a potential issue.
For example, in the assembly I would like to add a constraint that
states a particular property member of the class can not be equal to one
other property. In standard coding I can throw an exception during
run-time, but I would rather have the constraint work during compile to
throw an error or warning to the developer.

Other constraints might include:
- Property value type must be of typeA, typeB, or typeC.
- Property value can be of any type except Null.
- Property value must be one of structX member constants.

I've read a few articles that may suggest using System.Attributes to
accomplish this in the meta data, but the examples provided were
simplified for discussion purposes.

Can anyone point me to a good article or give an example of how I can
implement this type of behavior?

- Glen
 
I'm working on a custom assembly and I'm trying to figure out
the best approach to handling known constraints within the
assembly, once compiled, to alert the developer at compile time
of a potential issue. For example, in the assembly I would like
to add a constraint that states a particular property member of
the class can not be equal to one other property. In standard
coding I can throw an exception during run-time, but I would
rather have the constraint work during compile to throw an error
or warning to the developer.

Other constraints might include:
- Property value type must be of typeA, typeB, or typeC.
- Property value can be of any type except Null.
- Property value must be one of structX member constants.

I've read a few articles that may suggest using
System.Attributes to accomplish this in the meta data, but the
examples provided were simplified for discussion purposes.

Can anyone point me to a good article or give an example of how
I can implement this type of behavior?

Glen,

I think the issue isn't runtime constraint checking code vs.
attributes, but the limitations of what can realistically be done at
compile time, regardless of the mechanism used.

There are certain things the compiler simply cannot know at compile
time. The compiler can check type compatibility (enumerations are a
good construct to use here), and some value assignments. But the
compiler cannot check the majority of non-trivial assignments, even
simple ones like this:

// Directly assigning a number that's too large to fit into
// the value type results in a compile-time error.
byte a = 257;

// Basically the same operation, but since it's an indirect
// assignment, no comile-time error occurs.
// It's a run-time overflow error instead.
// (If overflow checking is turned on, that is. Otherwise,
// b will be assigned the value 1).
int i = 257;
byte b = (byte) i;

Reflection is another issue to consider. Once an assembly is built,
its properties can be changed at runtime by code the compiler had no
knowledge of. That means there's no way for the compiler to do the
kinds of compile-time value checks you're considering.

Regardless of what mechanism is used - normal constraint code or
attributes - checks like this will have to be done at run time.
 
Hi Chris,

Thanks for the response, and I agree that my logic was a bit off for the
scope of the compiler. I believe I've found basically what I'm looking
to do using enum and [Flags] attributes, so I'll play with that for a
while. It should give me at least some of the preprocessor control I
was looking for.

- Glen
 
Back
Top