Atribute inside class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Bazi

    #1

    Atribute inside class

    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.GetCust omAttributes( typeof(
    MyAttribute ), true );

    if(_AttributeMi ne.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.GetCust omAttributes( typeof( MyAttribute ), true );

    Is this a normal behaviour ?

    Thank you
  • Jon Skeet [C# MVP]

    #2
    Re: Atribute inside class

    Charles Bazi <cbazi@noos.frw rote:
    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.GetCust omAttributes( typeof(
    MyAttribute ), true );
    >
    if(_AttributeMi ne.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.GetCust omAttributes( 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.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Charles Bazi

      #3
      Re: Atribute inside class



      Jon Skeet [C# MVP] wrote:
      Charles Bazi <cbazi@noos.frw rote:
      >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.GetCust omAttributes( typeof(
      >MyAttribute ), true );
      >>
      >if(_AttributeM ine.Length == 1) {
      > _ClassType = ( _AttributeMine[0] as MyAttribute ).Name;
      >}
      >>
      >After debugging, I see _object being assigned with my class'type,
      >AttributeMin e is null after trying to load it with :
      >_object.GetCus tomAttributes( 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.
      >
      See http://www.pobox.com/~skeet/csharp/complete.html for details of
      what I mean by that.
      OK

      Comment

      • Charles Bazi

        #4
        Re: Atribute inside class [OK]

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

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

        Thank you again !!!

        Comment

        Working...