By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,527 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

Custom compile-time errors and warnings

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Glen <Bu****@hotmail.com> wrote in
news:uP**************@TK2MSFTNGP11.phx.gbl:
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.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2

P: n/a
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

Chris R. Timmons wrote:
Glen <Bu****@hotmail.com> wrote in
news:uP**************@TK2MSFTNGP11.phx.gbl:

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.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.