about attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    about attribute

    Hello!

    I have one simple question about attribute.
    According to the documentation will GetCustomAttrib utes return all the
    custom attributes for this assembly
    public virtual Object[] GetCustomAttrib utes (bool inherit)

    I just wonder if I have attribute on classes or on method will these be
    included and returned when using
    this GetCustomAttrib utes

    //Tony


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: about attribute

    Tony,

    Yes, they will, of course, what is returned depends on what you pass for
    the inherit parameter.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
    news:sB_Rk.3966 $U5.25974@newsb .telia.net...
    Hello!
    >
    I have one simple question about attribute.
    According to the documentation will GetCustomAttrib utes return all the
    custom attributes for this assembly
    public virtual Object[] GetCustomAttrib utes (bool inherit)
    >
    I just wonder if I have attribute on classes or on method will these be
    included and returned when using
    this GetCustomAttrib utes
    >
    //Tony
    >

    Comment

    • Tony Johansson

      #3
      Re: about attribute

      Hello!

      Here I have a simple example when I use this GetCustomAttrib utes but
      in this example GetCustomAttrib utes didn't returned the ObsoleteAttribu te
      that is used on the method
      Foo below.

      What is the reason that Obsolete was not returned ?


      class Program
      {
      static void Main(string[] args)
      {
      new Program().Foo() ;
      Assembly a =
      Assembly.LoadFr om(@"F:\C#\Cons oleApplication1 5\ConsoleApplic ation15\bin\Deb ug\ConsoleAppli cation15.exe");
      object[] attributes = a.GetCustomAttr ibutes(true);
      }

      [Obsolete("Just testing")]
      public void Foo()
      {}
      }


      //Tony


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omskrev i
      meddelandet news:eGMY7b2QJH A.4848@TK2MSFTN GP05.phx.gbl...
      Tony,
      >
      Yes, they will, of course, what is returned depends on what you pass
      for the inherit parameter.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
      news:sB_Rk.3966 $U5.25974@newsb .telia.net...
      >Hello!
      >>
      >I have one simple question about attribute.
      >According to the documentation will GetCustomAttrib utes return all the
      >custom attributes for this assembly
      >public virtual Object[] GetCustomAttrib utes (bool inherit)
      >>
      >I just wonder if I have attribute on classes or on method will these be
      >included and returned when using
      >this GetCustomAttrib utes
      >>
      >//Tony
      >>
      >
      >

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: about attribute

        You aren't seeing it because you are getting the custom attributes on
        the assembly, not on the method. If you want to get the attribute that is
        applied to the particular method, you have to get the MethodInfo instance
        and call GetCustomAttrib utes on that.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
        news:iT%Rk.3967 $U5.25908@newsb .telia.net...
        Hello!
        >
        Here I have a simple example when I use this GetCustomAttrib utes but
        in this example GetCustomAttrib utes didn't returned the ObsoleteAttribu te
        that is used on the method
        Foo below.
        >
        What is the reason that Obsolete was not returned ?
        >
        >
        class Program
        {
        static void Main(string[] args)
        {
        new Program().Foo() ;
        Assembly a =
        Assembly.LoadFr om(@"F:\C#\Cons oleApplication1 5\ConsoleApplic ation15\bin\Deb ug\ConsoleAppli cation15.exe");
        object[] attributes = a.GetCustomAttr ibutes(true);
        }
        >
        [Obsolete("Just testing")]
        public void Foo()
        {}
        }
        >
        >
        //Tony
        >
        >
        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omskrev i
        meddelandet news:eGMY7b2QJH A.4848@TK2MSFTN GP05.phx.gbl...
        >Tony,
        >>
        > Yes, they will, of course, what is returned depends on what you pass
        >for the inherit parameter.
        >>
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >>
        >"Tony Johansson" <johansson.ande rsson@telia.com wrote in message
        >news:sB_Rk.396 6$U5.25974@news b.telia.net...
        >>Hello!
        >>>
        >>I have one simple question about attribute.
        >>According to the documentation will GetCustomAttrib utes return all the
        >>custom attributes for this assembly
        >>public virtual Object[] GetCustomAttrib utes (bool inherit)
        >>>
        >>I just wonder if I have attribute on classes or on method will these be
        >>included and returned when using
        >>this GetCustomAttrib utes
        >>>
        >>//Tony
        >>>
        >>
        >>
        >
        >

        Comment

        • Tony Johansson

          #5
          Re: about attribute

          Hello!

          So what you are actually saying is that this use of GetCustomAttrib utes will
          only
          return the attributes for the assembly and not attribut for class and method
          and so on.

          static void Main(string[] args)
          {
          new Program().Foo() ;
          Assembly a =
          Assembly.LoadFr om(@"F:\C#\Cons oleApplication1 5\ConsoleApplic ation15\bin\Deb ug\ConsoleAppli cation15.exe");
          object[] attributes = a.GetCustomAttr ibutes(true);
          }



          //Tony

          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omskrev i
          meddelandet news:uc2PWt2QJH A.4760@TK2MSFTN GP02.phx.gbl...
          You aren't seeing it because you are getting the custom attributes on
          the assembly, not on the method. If you want to get the attribute that is
          applied to the particular method, you have to get the MethodInfo instance
          and call GetCustomAttrib utes on that.
          >
          >
          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m
          >
          "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
          news:iT%Rk.3967 $U5.25908@newsb .telia.net...
          >Hello!
          >>
          >Here I have a simple example when I use this GetCustomAttrib utes but
          >in this example GetCustomAttrib utes didn't returned the ObsoleteAttribu te
          >that is used on the method
          >Foo below.
          >>
          >What is the reason that Obsolete was not returned ?
          >>
          >>
          >class Program
          >{
          > static void Main(string[] args)
          > {
          > new Program().Foo() ;
          > Assembly a =
          >Assembly.LoadF rom(@"F:\C#\Con soleApplication 15\ConsoleAppli cation15\bin\De bug\ConsoleAppl ication15.exe") ;
          > object[] attributes = a.GetCustomAttr ibutes(true);
          > }
          >>
          > [Obsolete("Just testing")]
          > public void Foo()
          > {}
          >}
          >>
          >>
          >//Tony
          >>
          >>
          >"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omskrev
          >i meddelandet news:eGMY7b2QJH A.4848@TK2MSFTN GP05.phx.gbl...
          >>Tony,
          >>>
          >> Yes, they will, of course, what is returned depends on what you pass
          >>for the inherit parameter.
          >>>
          >>>
          >>--
          >> - Nicholas Paldino [.NET/C# MVP]
          >> - mvp@spam.guard. caspershouse.co m
          >>>
          >>"Tony Johansson" <johansson.ande rsson@telia.com wrote in message
          >>news:sB_Rk.39 66$U5.25974@new sb.telia.net...
          >>>Hello!
          >>>>
          >>>I have one simple question about attribute.
          >>>According to the documentation will GetCustomAttrib utes return all the
          >>>custom attributes for this assembly
          >>>public virtual Object[] GetCustomAttrib utes (bool inherit)
          >>>>
          >>>I just wonder if I have attribute on classes or on method will these be
          >>>included and returned when using
          >>>this GetCustomAttrib utes
          >>>>
          >>>//Tony
          >>>>
          >>>
          >>>
          >>
          >>
          >
          >

          Comment

          • Ignacio Machin ( .NET/ C# MVP )

            #6
            Re: about attribute

            On Nov 10, 12:56 pm, "Tony Johansson" <johansson.ande rs...@telia.com >
            wrote:
            Hello!
            >
            I have one simple question about attribute.
            According to the documentation will GetCustomAttrib utes return all the
            custom attributes for this assembly
            public virtual Object[] GetCustomAttrib utes (bool inherit)
            >
            I just wonder if I have attribute on classes or on method will these be
            included and returned when using
            this GetCustomAttrib utes
            >
            //Tony

            I don;t think so. The docs says nothing about it.
            You can always call the code for yourself and see what you get back :)

            Comment

            • Ignacio Machin ( .NET/ C# MVP )

              #7
              Re: about attribute

              On Nov 10, 3:02 pm, "Tony Johansson" <johansson.ande rs...@telia.com >
              wrote:
              Hello!
              >
              So what you are actually saying is that this use of GetCustomAttrib utes will
              only
              return the attributes for the assembly and not attribut for class and method
              and so on.
              Correct, you are getting the attributes of the assembly only. Note
              that the inherit is ignored (it's clearly stated in the docs)

              You will have to iterate in the types defined and then use
              Attribute.GetCu stomAttributes (Assembly, Type)

              Comment

              Working...