Groups | Blog | Home
all groups > dotnet general > february 2007 >

dotnet general : Atribute inside class


Jon Skeet [C# MVP]
2/7/2007 8:17:37 PM
[quoted text, click to view]

Um, what's the point of converting the type to a string and back again?
It's equivalent to

Type _object = GetType();

but somewhat less reliable (depending on whether the type of "this" is
declared in the currently executing assembly - that may not be the case
with inheritance involved).

[quoted text, click to view]

Well, you haven't shown us the code for MyAttribute or how you've used
it.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Charles Bazi
2/7/2007 8:59:36 PM
Hi, I have a class with custom attributes.

Aka [MyAttribute( "Joe")]

I try to get value of my attribute, but inside the class where it's
declared with this piece of code:

string _ClassType = string.Empty;

Type _object = Type.GetType( this.GetType().ToString() );

object[] _AttributeMine = _object.GetCustomAttributes( typeof(
MyAttribute ), true );

if(_AttributeMine.Length == 1) {
_ClassType = ( _AttributeMine[0] as MyAttribute ).Name;
}

After debugging, I see _object being assigned with my class'type,
AttributeMine is null after trying to load it with :
_object.GetCustomAttributes( typeof( MyAttribute ), true );

Is this a normal behaviour ?

Charles Bazi
2/7/2007 9:37:37 PM


[quoted text, click to view]

Yes :|
[quoted text, click to view]

MyAttribute's only purpose is to hold the value of Name, and I have this
code working, when used outside the class
[quoted text, click to view]

There is no more program, just need to get the value of the attribute
inside the class. I want to do it this way for mantainabilty issues...

Thank you.
[quoted text, click to view]

Charles Bazi
2/7/2007 10:00:59 PM
[quoted text, click to view]

So right for that !!!

My error: I was trying to get attributes from properties, but my
attribute is for the constructor.

AddThis Social Bookmark Button