473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Custom compile-time errors and warnings

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
2 4610
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob Meade | last post by:
Hi all, I have created 7 web custom controls for 7 sections of our template. I have them currently on my local pc - where vs placed them...the plan was that I would create these - throw them...
3
by: David Freeman | last post by:
Hi There! I'm having trouble dynamically adding custom controls. My custom controls does not use code-behind but only <script /> block for programming. So as a result, I don't need to compile my...
3
by: Eric Sabine | last post by:
I've created a custom control (ascx) which contains a tree view, a drop down list, and a listbox. The custom control is included in a web form and at runtime, won't compile (I get the...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
4
by: Matt Jensen | last post by:
Howdy Relatively new to .NET, I'm trying to create custom a namespace for use in creating some utility classes, which I seem to have done OK, however, I'm having a problem trying to use the class...
1
by: Mutley | last post by:
Hi, I have written an HttpHandler to process page requests for a custom file extension. The handler gets called as expected and after searching the web for examples I have used a method called...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
4
by: Daniel Regalia | last post by:
Happy monday to all who read this... How would I go about creating a custom dll in VB.net 2 that I can use to make custom functions (extended stored procedures) for SQL2000/2005? Is there an...
0
by: jwriteclub | last post by:
I feel like this must be addressed somewhere, but I cannot seem to find a good explanation, so a link somewhere (or an explanation) would be much appreciated. Using Adobe Flex Builder 2.0.1. I...
3
by: JJ | last post by:
Whats the best way to convert a user control to a custom server control? i.e. I created a .aspx control using the designer to create the look of the control. I now want to use this in other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.