System.Attribute Class
Information   Base Types   Related Resources

Base class for custom attributes.

  • Namespace: System
  • First seen in: .NET v1.0.3705
  • Last seen in: .NET v1.1.4322
  • Last changed in: .NET v1.0.3705
  • Assembly: mscorlib.dll

  • System.Object
  • View this type on MSDN
  • View this type on WinFX 247
    Articles (16)Discussions (106)MembersRotorChanges
    Rotor

    Rotor explanation...

    1: // ==++==
    2: //
    3: //
    4: // Copyright (c) 2002 Microsoft Corporation. All rights reserved.
    5: //
    6: // The use and distribution terms for this software are contained in the file
    7: // named license.txt, which can be found in the root of this distribution.
    8: // By using this software in any fashion, you are agreeing to be bound by the
    9: // terms of this license.
    10: //
    11: // You must not remove this notice, or any other, from this software.
    12: //
    13: //
    14: // ==--==
    15: /*============================================================
    16: **
    17: ** Class: Attribute
    18: **
    19: **
    20: **
    21: ** Purpose: The class used as an attribute to denote that
    22: ** another class can be used as an attribute.
    23: **
    24: ** Date: December 7, 1999
    25: **
    26: ===========================================================*/
    27: namespace System {
    28:
    29:     using System;
    30:     using System.Reflection;
    31:     using System.Collections;
    32:
    33:     /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute"]/*' />
    34:     [Serializable, AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple=false)] // Base class for all attributes
    35:     public abstract class Attribute {
    36:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.Attribute"]/*' />
    37:         protected Attribute(){}
    38:
    39:         // This is a private enum used solely for the purpose of avoiding code repeat for these types
    40:


    GetCustomAttributes - Info | MSDN | Search
    41:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes8"]/*' />
    42:         public static Attribute[] GetCustomAttributes(MemberInfo element, Type type)
    43:         {
    44:             return GetCustomAttributes(element, type, true);
    45:         }
    46:         

    GetCustomAttributes -
    Info | MSDN | Search
    47:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes"]/*' />
    48:         public static Attribute[] GetCustomAttributes(MemberInfo element, Type type, bool inherit)
    49:         {
    50:             if (element == null)
    51:                 throw new ArgumentNullException("element");
    52:
    53:             if (type == null)
    54:                 throw new ArgumentNullException("type");
    55:             
    56:             if (!type.IsSubclassOf(typeof(Attribute)) && type != typeof(Attribute))
    57:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    58:
    59:             Object[] attributes = null;
    60:             switch(element.MemberType)
    61:             {
    62:                 case MemberTypes.Method:
    63:                     attributes = ((MethodInfo)element).GetCustomAttributes(type, inherit);
    64:                     break;
    65:
    66:                 case MemberTypes.TypeInfo:
    67:                 case MemberTypes.NestedType:
    68:                     attributes = ((Type)element).GetCustomAttributes(type, inherit);
    69:                     break;
    70:
    71:                 case MemberTypes.Constructor:
    72:                     attributes = ((ConstructorInfo)element).GetCustomAttributes(type, inherit);
    73:                     break;
    74:                     
    75:                 case MemberTypes.Field:
    76:                     attributes = ((FieldInfo)element).GetCustomAttributes(type, inherit);
    77:                     break;
    78:
    79:                 case MemberTypes.Property:
    80:                     return InternalGetCustomAttributes((PropertyInfo)element, type, inherit);
    81:
    82:                 case MemberTypes.Event:
    83:                     return InternalGetCustomAttributes((EventInfo)element, type, inherit);
    84:
    85:                 default:
    86:                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnsupportedMemberInfoTypes"));
    87:             }
    88:             return (Attribute[])attributes;
    89:         }
    90:

    GetCustomAttributes -
    Info | MSDN | Search
    91:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes9"]/*' />
    92:         public static Attribute[] GetCustomAttributes(MemberInfo element)
    93:         {
    94:             return GetCustomAttributes(element, true);
    95:         }
    96:

    GetCustomAttributes -
    Info | MSDN | Search
    97:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes1"]/*' />
    98:         public static Attribute[] GetCustomAttributes(MemberInfo element, bool inherit)
    99:         {
    100:             if (element == null)
    101:                 throw new ArgumentNullException("element");
    102:
    103:             Object[] attributes = null;
    104:             switch(element.MemberType)
    105:             {
    106:                 case MemberTypes.Method:
    107:                     attributes = ((MethodInfo)element).GetCustomAttributes(s_AttributeType, inherit);
    108:                     break;
    109:
    110:                 case MemberTypes.TypeInfo:
    111:                 case MemberTypes.NestedType:
    112:                     attributes = ((Type)element).GetCustomAttributes(s_AttributeType, inherit);
    113:                     break;
    114:
    115:                 case MemberTypes.Constructor:
    116:                     attributes = ((ConstructorInfo)element).GetCustomAttributes(s_AttributeType, inherit);
    117:                     break;
    118:                     
    119:                 case MemberTypes.Field:
    120:                     attributes = ((FieldInfo)element).GetCustomAttributes(s_AttributeType, inherit);
    121:                     break;
    122:
    123:                 case MemberTypes.Property:
    124:                     return InternalGetCustomAttributes((PropertyInfo)element, s_AttributeType, inherit);
    125:
    126:                 case MemberTypes.Event:
    127:                     return InternalGetCustomAttributes((EventInfo)element, s_AttributeType, inherit);
    128:
    129:                 default:
    130:                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnsupportedMemberInfoTypes"));
    131:             }
    132:             return (Attribute[])attributes;
    133:         }
    134:         

    InternalGetCustomAttributes -
    Info | MSDN | Search
    135:         private static Attribute[] InternalGetCustomAttributes(PropertyInfo element, Type type, bool inherit)
    136:         {
    137:             // walk up the hierarchy chain
    138:             Attribute[] attributes = (Attribute[])element.GetCustomAttributes(type, inherit);
    139:             if (inherit) {
    140:                 // create the hashtable that keeps track of inherited types
    141:                 Hashtable types = new Hashtable(11);
    142:                 // create an array list to collect all the requested attibutes
    143:                 ArrayList attributeList = new ArrayList();
    144:                 CopyToArrayList(attributeList, attributes, types);
    145:
    146:                 PropertyInfo baseProp = GetParentDefinition(element);
    147:                 while (baseProp != null) {
    148:                     attributes = GetCustomAttributes(baseProp, type, false);
    149:                     AddAttributesToList(attributeList, attributes, types);
    150:                     baseProp = GetParentDefinition(baseProp);
    151:                 }
    152:                 return (Attribute[])attributeList.ToArray(type);
    153:             }
    154:             else
    155:                 return attributes;
    156:         }
    157:

    InternalGetCustomAttributes -
    Info | MSDN | Search
    158:         private static Attribute[] InternalGetCustomAttributes(EventInfo element, Type type, bool inherit)
    159:         {
    160:             // walk up the hierarchy chain
    161:             Attribute[] attributes = (Attribute[])element.GetCustomAttributes(type, inherit);
    162:             if (inherit) {
    163:                 // create the hashtable that keeps track of inherited types
    164:                 Hashtable types = new Hashtable(11);
    165:                 // create an array list to collect all the requested attibutes
    166:                 ArrayList attributeList = new ArrayList();
    167:                 CopyToArrayList(attributeList, attributes, types);
    168:
    169:                 EventInfo baseEvent = GetParentDefinition(element);
    170:                 while (baseEvent != null) {
    171:                     attributes = GetCustomAttributes(baseEvent, type, false);
    172:                     AddAttributesToList(attributeList, attributes, types);
    173:                     baseEvent = GetParentDefinition(baseEvent);
    174:                 }
    175:                 return (Attribute[])attributeList.ToArray(type);
    176:             }
    177:             else
    178:                 return attributes;
    179:         }
    180:

    CopyToArrayList -
    Info | MSDN | Search
    181:         //
    182:         // utility functions
    183:         //
    184:         static private void CopyToArrayList(ArrayList attributeList, Attribute[] attributes, Hashtable types) {
    185:             for (int i = 0; i < attributes.Length; i++) {
    186:                 attributeList.Add(attributes[i]);
    187:                 Type attrType = attributes[i].GetType();
    188:                 if (!types.Contains(attrType))
    189:                     types[attrType] = InternalGetAttributeUsage(attrType);
    190:             }
    191:         }
    192:         

    AddAttributesToList -
    Info | MSDN | Search
    193:         static private void AddAttributesToList(ArrayList attributeList, Attribute[] attributes, Hashtable types) {
    194:             for (int i = 0; i < attributes.Length; i++) {
    195:                 Type attrType = attributes[i].GetType();
    196:                 AttributeUsageAttribute usage = (AttributeUsageAttribute)types[attrType];
    197:                 if (usage == null) {
    198:                     // the type has never been seen before if it's inheritable add it to the list
    199:                     usage = InternalGetAttributeUsage(attrType);
    200:                     types[attrType] = usage;
    201:                     if (usage.Inherited)
    202:                         attributeList.Add(attributes[i]);
    203:                 }
    204:                 else if (usage.Inherited && usage.AllowMultiple)
    205:                     // we saw this type already add it only if it is inheritable and it does allow multiple
    206:                     attributeList.Add(attributes[i]);
    207:             }
    208:         }
    209:

    GetCustomAttributes -
    Info | MSDN | Search
    210:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes10"]/*' />
    211:         public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType)
    212:         {
    213:             return (Attribute[])GetCustomAttributes (element, attributeType, true);
    214:         }
    215:

    GetCustomAttributes -
    Info | MSDN | Search
    216:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes2"]/*' />
    217:         public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType, bool inherit)
    218:         {
    219:             if (element == null)
    220:                 throw new ArgumentNullException("element");
    221:
    222:             if (attributeType == null)
    223:                 throw new ArgumentNullException("attributeType");
    224:             
    225:             if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
    226:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    227:
    228:             MemberInfo member = element.Member;
    229:             if (member.MemberType == MemberTypes.Method && inherit)
    230:                 return InternalParamGetCustomAttributes((MethodInfo)member, element, attributeType, inherit);
    231:
    232:             return (Attribute[])element.GetCustomAttributes(attributeType, inherit);
    233:         }
    234:

    InternalParamGetCustomAttributes -
    Info | MSDN | Search
    235:         // For ParameterInfo's we need to make sure that we chain through all the MethodInfo's in the inheritance chain that
    236:         // have this ParameterInfo defined. .We pick up all the CustomAttributes for the starting ParameterInfo. We need to pick up only attributes
    237:         // that are marked inherited from the remainder of the MethodInfo's in the inheritance chain.
    238:         // For MethodInfo's on an interface we do not do an inheritance walk so the default ParameterInfo attributes are returned.
    239:         // For MethodInfo's on a class we walk up the inheritance chain but do not look at the MethodInfo's on the interfaces that the
    240:         // class inherits from and return the respective ParameterInfo attributes
    241:         private static Attribute[] InternalParamGetCustomAttributes(MethodInfo method, ParameterInfo param, Type type, bool inherit)
    242:         {
    243:
    244:             ArrayList disAllowMultiple = new ArrayList();
    245:             Object [] objAttr;
    246:
    247:             if (type == null)
    248:                 type = s_AttributeType;
    249:
    250:             objAttr = param.GetCustomAttributes(type, false);
    251:                 
    252:             for (int i=0;i<objAttr.Length;i++)
    253:             {
    254:                 Type objType = objAttr[i].GetType();
    255:                 AttributeUsageAttribute attribUsage = InternalGetAttributeUsage(objType);
    256:                 if (attribUsage.AllowMultiple == false)
    257:                     disAllowMultiple.Add(objType);
    258:             }
    259:
    260:             // Get all the attributes that have Attribute as the base class
    261:             Attribute [] ret = null;
    262:             if (objAttr.Length == 0)
    263:                 ret = (Attribute[])Array.CreateInstance(type,0);
    264:             else
    265:                 ret = (Attribute[])objAttr;
    266:             
    267:             if (method.DeclaringType == null) // This is an interface so we are done.
    268:                 return ret;
    269:             
    270:             if (!inherit)
    271:                 return ret;
    272:         
    273:             int paramPosition = param.Position;
    274:             method = method.GetParentDefinition();
    275:             
    276:             while (method != null)
    277:             {
    278:                 // Find the ParameterInfo on this method
    279:                 ParameterInfo [] parameters = method.GetParameters();
    280:                 param = parameters[paramPosition]; // Point to the correct ParameterInfo of the method
    281:
    282:                 objAttr = param.GetCustomAttributes(type, false);
    283:                 
    284:                 int count = 0;
    285:                 for (int i=0;i<objAttr.Length;i++)
    286:                 {
    287:                     Type objType = objAttr[i].GetType();
    288:                     AttributeUsageAttribute attribUsage = InternalGetAttributeUsage(objType);
    289:
    290:                     if ((attribUsage.Inherited)
    291:                         && (disAllowMultiple.Contains(objType) == false))
    292:                         {
    293:                             if (attribUsage.AllowMultiple == false)
    294:                                 disAllowMultiple.Add(objType);
    295:                             count++;
    296:                         }
    297:                     else
    298:                         objAttr[i] = null;
    299:                 }
    300:
    301:                 // Get all the attributes that have Attribute as the base class
    302:                 Attribute [] attributes = (Attribute[])Array.CreateInstance(type,count);
    303:                 
    304:                 count=0;
    305:                 for (int i=0;i<objAttr.Length;i++)
    306:                 {
    307:                     if (objAttr[i] != null)
    308:                     {
    309:                         attributes[count] = (Attribute)objAttr[i];
    310:                         count++;
    311:                     }
    312:                 }
    313:                 
    314:                 Attribute [] temp = ret;
    315:                 ret = (Attribute[])Array.CreateInstance(type,temp.Length + count);
    316:                 Array.Copy(temp,ret,temp.Length);
    317:                 
    318:                 int offset = temp.Length;
    319:
    320:                 for (int i=0;i<attributes.Length;i++)
    321:                     ret[offset + i] = attributes[i];
    322:     
    323:                 method = method.GetParentDefinition();
    324:                 
    325:             }
    326:
    327:             return ret;
    328:         
    329:         }
    330:

    GetCustomAttributes -
    Info | MSDN | Search
    331:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes11"]/*' />
    332:         public static Attribute[] GetCustomAttributes (Module element, Type attributeType)
    333:         {
    334:             return GetCustomAttributes (element, attributeType, true);
    335:         }
    336:

    GetCustomAttributes -
    Info | MSDN | Search
    337:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes3"]/*' />
    338:         public static Attribute[] GetCustomAttributes (Module element, Type attributeType, bool inherit)
    339:         {
    340:             if (element == null)
    341:                 throw new ArgumentNullException("element");
    342:
    343:             if (attributeType == null)
    344:                 throw new ArgumentNullException("attributeType");
    345:
    346:             if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
    347:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    348:
    349:             return (Attribute[])element.GetCustomAttributes(attributeType, inherit);
    350:         }
    351:

    GetCustomAttributes -
    Info | MSDN | Search
    352:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes12"]/*' />
    353:         public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType)
    354:         {
    355:             return GetCustomAttributes (element, attributeType, true);
    356:         }
    357:

    GetCustomAttributes -
    Info | MSDN | Search
    358:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes4"]/*' />
    359:         public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType, bool inherit)
    360:         {
    361:             if (element == null)
    362:                 throw new ArgumentNullException("element");
    363:
    364:             if (attributeType == null)
    365:                 throw new ArgumentNullException("attributeType");
    366:
    367:             if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
    368:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    369:
    370:             return (Attribute[])element.GetCustomAttributes(attributeType, inherit);
    371:         }
    372:

    GetCustomAttributes -
    Info | MSDN | Search
    373:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes13"]/*' />
    374:         public static Attribute[] GetCustomAttributes(ParameterInfo element)
    375:         {
    376:             return GetCustomAttributes(element, true);
    377:         }
    378:         

    GetCustomAttributes -
    Info | MSDN | Search
    379:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes5"]/*' />
    380:         public static Attribute[] GetCustomAttributes(ParameterInfo element, bool inherit)
    381:         {
    382:             if (element == null)
    383:                 throw new ArgumentNullException("element");
    384:
    385:             MemberInfo member = element.Member;
    386:             if (member.MemberType == MemberTypes.Method && inherit)
    387:                 return InternalParamGetCustomAttributes((MethodInfo)member, element, null, inherit);
    388:             
    389:             return (Attribute[])element.GetCustomAttributes(s_AttributeType, inherit);
    390:         }
    391:

    GetCustomAttributes -
    Info | MSDN | Search
    392:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes14"]/*' />
    393:         public static Attribute[] GetCustomAttributes(Module element)
    394:         {
    395:             return GetCustomAttributes(element, true);
    396:         }
    397:

    GetCustomAttributes -
    Info | MSDN | Search
    398:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes6"]/*' />
    399:         public static Attribute[] GetCustomAttributes(Module element, bool inherit)
    400:         {
    401:             if (element == null)
    402:                 throw new ArgumentNullException("element");
    403:
    404:             return (Attribute[])element.GetCustomAttributes(s_AttributeType, inherit);
    405:         }
    406:

    GetCustomAttributes -
    Info | MSDN | Search
    407:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes15"]/*' />
    408:         public static Attribute[] GetCustomAttributes(Assembly element)
    409:         {
    410:             return GetCustomAttributes(element, true);
    411:         }
    412:

    GetCustomAttributes -
    Info | MSDN | Search
    413:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.GetCustomAttributes7"]/*' />
    414:         public static Attribute[] GetCustomAttributes(Assembly element, bool inherit)
    415:         {
    416:             if (element == null)
    417:                 throw new ArgumentNullException("element");
    418:
    419:             return (Attribute[])element.GetCustomAttributes(s_AttributeType, inherit);
    420:         }
    421:

    IsDefined -
    Info | MSDN | Search
    422:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined4"]/*' />
    423:         public static bool IsDefined (MemberInfo element, Type attributeType)
    424:         {
    425:             return IsDefined(element, attributeType, true);
    426:         }
    427:

    IsDefined -
    Info | MSDN | Search
    428:         // Returns true if a custom attribute subclass of attributeType class/interface with inheritance walk
    429:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined"]/*' />
    430:         public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit)
    431:         {
    432:             if (element == null)
    433:                 throw new ArgumentNullException("element");
    434:
    435:             if (attributeType == null)
    436:                 throw new ArgumentNullException("attributeType");
    437:             
    438:             if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
    439:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    440:
    441:             switch(element.MemberType)
    442:             {
    443:                 case MemberTypes.Method:
    444:                     return ((MethodInfo)element).IsDefined(attributeType,inherit);
    445:
    446:                 case MemberTypes.TypeInfo:
    447:                 case MemberTypes.NestedType:
    448:                     return ((Type)element).IsDefined(attributeType,inherit);
    449:
    450:                 case MemberTypes.Constructor:
    451:                     return ((ConstructorInfo)element).IsDefined(attributeType,false);
    452:                     
    453:                 case MemberTypes.Field:
    454:                     return ((FieldInfo)element).IsDefined(attributeType,false);
    455:
    456:                 case MemberTypes.Property:
    457:                     return InternalIsDefined((PropertyInfo)element,attributeType,false);
    458:
    459:                 case MemberTypes.Event:
    460:                     return InternalIsDefined((EventInfo)element,attributeType,false);
    461:
    462:                 default:
    463:                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnsupportedMemberInfoTypes"));
    464:             }
    465:
    466:         }
    467:

    InternalIsDefined -
    Info | MSDN | Search
    468:         private static bool InternalIsDefined (PropertyInfo element, Type attributeType, bool inherit)
    469:         {
    470:             // walk up the hierarchy chain
    471:             if (element.IsDefined(attributeType, inherit))
    472:                 return true;
    473:             
    474:             if (inherit) {
    475:                 AttributeUsageAttribute usage = InternalGetAttributeUsage(attributeType);
    476:                 if (!usage.Inherited)
    477:                     return false;
    478:                 PropertyInfo baseProp = GetParentDefinition(element);
    479:                 while (baseProp != null) {
    480:                     if (baseProp.IsDefined(attributeType, false))
    481:                         return true;
    482:                     baseProp = GetParentDefinition(baseProp);
    483:                 }
    484:             }
    485:             return false;
    486:         }
    487:

    InternalIsDefined -
    Info | MSDN | Search
    488:         private static bool InternalIsDefined (EventInfo element, Type attributeType, bool inherit)
    489:         {
    490:             // walk up the hierarchy chain
    491:             if (element.IsDefined(attributeType, inherit))
    492:                 return true;
    493:             
    494:             if (inherit) {
    495:                 AttributeUsageAttribute usage = InternalGetAttributeUsage(attributeType);
    496:                 if (!usage.Inherited)
    497:                     return false;
    498:                 EventInfo baseEvent = GetParentDefinition(element);
    499:                 while (baseEvent != null) {
    500:                     if (baseEvent.IsDefined(attributeType, false))
    501:                         return true;
    502:                     baseEvent = GetParentDefinition(baseEvent);
    503:                 }
    504:             }
    505:             return false;
    506:         }
    507:

    IsDefined -
    Info | MSDN | Search
    508:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined5"]/*' />
    509:         public static bool IsDefined (ParameterInfo element, Type attributeType)
    510:         {
    511:             return IsDefined(element, attributeType, true);
    512:         }
    513:

    IsDefined -
    Info | MSDN | Search
    514:         // Returns true is a custom attribute subclass of attributeType class/interface with inheritance walk
    515:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined1"]/*' />
    516:         public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit)
    517:         {
    518:             if (element == null)
    519:                 throw new ArgumentNullException("element");
    520:
    521:             if (attributeType == null)
    522:                 throw new ArgumentNullException("attributeType");
    523:             
    524:             if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
    525:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    526:
    527:             MemberInfo member = element.Member;
    528:
    529:             switch(member.MemberType)
    530:             {
    531:                 case MemberTypes.Method: // We need to climb up the member hierarchy
    532:                     return InternalParamIsDefined((MethodInfo)member,element,attributeType,inherit);
    533:
    534:                 case MemberTypes.Constructor:
    535:                     return element.IsDefined(attributeType,false);
    536:
    537:                 case MemberTypes.Property:
    538:                     return element.IsDefined(attributeType,false);
    539:
    540:                 default:
    541:                     BCLDebug.Assert(false,"Invalid type for ParameterInfo member in Attribute class");
    542:                     throw new ArgumentException(Environment.GetResourceString("Argument_InvalidParamInfo"));
    543:             }
    544:         }
    545:

    IsDefined -
    Info | MSDN | Search
    546:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined6"]/*' />
    547:         public static bool IsDefined (Module element, Type attributeType)
    548:         {
    549:             return IsDefined(element, attributeType, false);
    550:         }
    551:

    IsDefined -
    Info | MSDN | Search
    552:         // Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk
    553:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined2"]/*' />
    554:         public static bool IsDefined (Module element, Type attributeType, bool inherit)
    555:         {
    556:             if (element == null)
    557:                 throw new ArgumentNullException("element");
    558:
    559:             if (attributeType == null)
    560:                 throw new ArgumentNullException("attributeType");
    561:
    562:             if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
    563:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    564:
    565:             return element.IsDefined(attributeType,false);
    566:         }
    567:

    IsDefined -
    Info | MSDN | Search
    568:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined7"]/*' />
    569:         public static bool IsDefined (Assembly element, Type attributeType)
    570:         {
    571:             return IsDefined (element, attributeType, true);
    572:         }
    573:

    IsDefined -
    Info | MSDN | Search
    574:         // Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk
    575:         /// <include file='doc\Attribute.uex' path='docs/doc[@for="Attribute.IsDefined3"]/*' />
    576:         public static bool IsDefined (Assembly element, Type attributeType, bool inherit)
    577:         {
    578:             if (element == null)
    579:                 throw new ArgumentNullException("element");
    580:
    581:             if (attributeType == null)
    582:                 throw new ArgumentNullException("attributeType");
    583:
    584:             if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
    585:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
    586:
    587:             return element.IsDefined(attributeType, false);
    588:         }
    589:
    590:

    InternalParamIsDefined -
    Info | MSDN | Search
    591:         // For ParameterInfo's we need to make sure that we chain through all the MethodInfo's in the inheritance chain.
    592:         // We pick up all the CustomAttributes for the starting ParameterInfo. We need to pick up only attributes that are marked inherited from the remainder of the ParameterInfo's in the inheritance chain.
    593:         // For MethodInfo's on an interface we do not do an inheritance walk. For ParameterInfo's on a
    594:         // Class we walk up the inheritance chain but do not look at the MethodInfo's on the interfaces that the
    595:         // class inherits from.
    596:         private static bool InternalParamIsDefined(MethodInfo method,ParameterInfo param,Type type, bool inherit)
    597:         {
    598:             if (param.IsDefined(type, false))
    599:                 return true;
    600:             
    601:             if (method.DeclaringType == null || !inherit) // This is an interface so we are done.
    602:                 return false;
    603:         
    604:             int paramPosition = param.Position;
    605:             method = method.GetParentDefinition();
    606:                         
    607:             while (method != null)
    608:             {
    609:                 ParameterInfo [] parameters = method.GetParameters();
    610:                 param = parameters[paramPosition];
    611:
    612:                 Object [] objAttr = param.GetCustomAttributes(type, false);
    613:                                 
    614:                 for (int i=0;i<objAttr.Length;i++)
    615:                 {
    616:                     Type objType = objAttr[i].GetType();
    617:                     AttributeUsageAttribute attribUsage = InternalGetAttributeUsage(objType);
    618:
    619:                     if ((objAttr[i] is Attribute)
    620:                             && (attribUsage.Inherited))
    621:                             return true;
    622:                 }
    623:                     
    624:                 method = method.GetParentDefinition();
    625:                 
    626:             }
    627:
    628:             return false;
    629:         }
    630:
    631:         

    InternalGetAttributeUsage -
    Info | MSDN | Search
    632:         // Check if the custom attributes is Inheritable
    633:         private static AttributeUsageAttribute InternalGetAttributeUsage(Type type)
    634:         {
    635:             Object [] obj = type.GetCustomAttributes(s_AttributeUsageType, false);
    636:             AttributeUsageAttribute attrib;
    637:             if (obj.Length == 1)
    638:                 attrib = (AttributeUsageAttribute)obj[0];
    639:             else
    640:             if (obj.Length == 0)
    641:                 attrib = AttributeUsageAttribute.Default;
    642:             else
    643:                 throw new FormatException(Environment.GetResourceString("Format_AttributeUsage"));
    644:        &