locked
identifying class level attributes RRS feed

  • Question

  • How would you do this? I saw an example on the forum for identifying attributes applied to members but when I look at Attributes at the TypeNode level it's empty. Where am I going to find this information?

    Thanks.
    Monday, September 12, 2005 8:40 PM

Answers

  • Attributes applied to type are, in fact, available at TypeNode.Attributes. Are you sure you were examining a type with attribute information?
    Tuesday, September 13, 2005 2:01 PM
  • Ah. Not everything that's rendered with an attribute syntax is actually emitted as attribute data in IL. You've put your finger on one here. In this case, there is a bit in the flags associated with a type that marks it as serializable. This is done for performance reasons, no doubt.

    The [DllImport] attribute is another example of something that doesn't compile to actual custom attribute metadata.

    You hit on the right solution, though, call our RuleUtilities.IsSerializable helper, which does the appropriate magic.
    Tuesday, September 13, 2005 4:35 PM

All replies

  • Attributes applied to type are, in fact, available at TypeNode.Attributes. Are you sure you were examining a type with attribute information?
    Tuesday, September 13, 2005 2:01 PM
  • I'm pretty sure I was. Here is the example:



       [Serializable()]
        public class RB8_19 : ISerializable
        {
           public RB8_19(){}
           public void GetObjectData(SerializationInfo info, StreamingContext context){}
        }
        // Am I missing anything?

     


    In my rule code I was checking TypeNode.Attributes and the AttributeList was empty. I ended up using one of the default rules from FxCop as an example and saw this: RuleUtilities.IsSerializable. That call and some other checks accomplished my goal.
    Tuesday, September 13, 2005 3:22 PM
  • Ah. Not everything that's rendered with an attribute syntax is actually emitted as attribute data in IL. You've put your finger on one here. In this case, there is a bit in the flags associated with a type that marks it as serializable. This is done for performance reasons, no doubt.

    The [DllImport] attribute is another example of something that doesn't compile to actual custom attribute metadata.

    You hit on the right solution, though, call our RuleUtilities.IsSerializable helper, which does the appropriate magic.
    Tuesday, September 13, 2005 4:35 PM