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.
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.