Attribute Construction/Validation, How?

A

Anthony Walter

Hi,

I am trying to writing my own custom attribute classes and would like to
have the same level of functionality as some of the attribute classes that
ship with the .NET Framework.

Specifically I would like to be able to take action with my attribute at
compile time.

For example, the Guid Atribute will give you this error at compile time if
you attempt to using it with an improperly formatted string:

[Guid("garbage string")]
public class Test
{
}

Yields the compiler error:
'Incorrect uuid format.'

Another example of attributes taking action at compile time is the
ComImportAttribute. Attempting to use this attribute by itself causes the
following to happen:

[ComImport]
public class AnotherTest
{
}

Yields the following two compiler errors:
'The Guid attribute must be specified with the ComImport attribute'
'A class with the ComImport attribute cannot have a user-defined
constructor'

I would really like to have this kind of flexibility with my own custom
attributes. Is this at all possible?

I sadly suspect the answer is no. As a matter of fact, it seems as if user
defined attributes can only be accessed (and only invoked once reflected) at
runtime.

If this is not the case, could someone explain how it is possible to take
action with user defined attribute at compile time, and even load time?

Thanks in advance.
 
M

Mattias Sjögren

Anthony,
I would really like to have this kind of flexibility with my own custom
attributes. Is this at all possible?

No. The checks you mention above are implemented in the compiler, not
in the attribute classes.



Mattias
 
A

Anthony Walter

I would really like to have this kind of flexibility with my own custom
No. The checks you mention above are implemented in the compiler, not
in the attribute classes.


Okay,

But would it be beneficial if sometime in the future Microsoft included this
level of functionality for user defined attributes? I think it could be
exposed to the programmer through (suprise) custom attributes.

For example a Microsoft supplied:

[AttributeUsage(AttributeTargets.Constructor)]
class CompileCheckAttribute : Attribute
{
public CompileCheckAttribute(string validator)
}

Might cause the compiler to do both of the following, invoke a method at
compile time and strip it out the attribute and validator method from the
compiled code:

namespace YourCompany
{

public ClsidAttribute : Attribute
{
private void Validator(object[] args)
{
try
{
Guid clsid = new Guid((string)args[0]);
}
catch
{
throw new CompilerException("Invalid uuid format".);
}
}
[CompileCheck("Validator")]
public Clsid(string clsid)
{
...
}
...
}

}

And then checked with:

[Clid("garbage paramter")]
interface ISomeInterface
{
}

Yielding the compiler error:

Error emitting 'YourCompany.ClsidAttribute' attribute -- 'Incorrect uuid
format.'

What do you think about this approach? If this idea sounds reasonable to
enough people, could we please forward it to the approriate Microsoft
feature request group?

Thanks in advance again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top