Atribute inside class

C

Charles Bazi

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 ?

Thank you
 
J

Jon Skeet [C# MVP]

Charles Bazi said:
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() );

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

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

Charles Bazi

Jon said:
Charles Bazi said:
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() );

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

Type _object = GetType();

Yes :|
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).
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 ?

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

MyAttribute's only purpose is to hold the value of Name, and I have this
code working, when used outside the class
Could you post a short but complete program which demonstrates the
problem?

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.

OK
 

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