diff options
Diffstat (limited to 'src/mscorlib/src/System/Attribute.cs')
-rw-r--r-- | src/mscorlib/src/System/Attribute.cs | 204 |
1 files changed, 102 insertions, 102 deletions
diff --git a/src/mscorlib/src/System/Attribute.cs b/src/mscorlib/src/System/Attribute.cs index b44e13f959..baa9a71638 100644 --- a/src/mscorlib/src/System/Attribute.cs +++ b/src/mscorlib/src/System/Attribute.cs @@ -3,19 +3,20 @@ // See the LICENSE file in the project root for more information. -namespace System { - - using System; - using System.Reflection; - using System.Collections.Generic; - using System.Runtime.InteropServices; - using System.Globalization; - using System.Diagnostics; - using System.Diagnostics.Contracts; - using System.Security; +using System; +using System.Reflection; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Globalization; +using System.Diagnostics; +using System.Diagnostics.Contracts; +using System.Security; + +namespace System +{ [Serializable] - [AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple=false)] + [AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple = false)] public abstract class Attribute { #region Private Statics @@ -42,7 +43,7 @@ namespace System { //if this is an index we need to get the parameter types to help disambiguate Type[] indexParamTypes = GetIndexParameterTypes(element); - + PropertyInfo baseProp = GetParentDefinition(element, indexParamTypes); while (baseProp != null) @@ -61,12 +62,12 @@ namespace System { // walk up the hierarchy chain if (element.IsDefined(attributeType, inherit)) return true; - + if (inherit) { AttributeUsageAttribute usage = InternalGetAttributeUsage(attributeType); - if (!usage.Inherited) + if (!usage.Inherited) return false; //if this is an index we need to get the parameter types to help disambiguate @@ -92,9 +93,9 @@ namespace System { // for the current property get the base class of the getter and the setter, they might be different // note that this only works for RuntimeMethodInfo - MethodInfo propAccessor = property.GetGetMethod(true); + MethodInfo propAccessor = property.GetGetMethod(true); - if (propAccessor == null) + if (propAccessor == null) propAccessor = property.GetSetMethod(true); RuntimeMethodInfo rtPropAccessor = propAccessor as RuntimeMethodInfo; @@ -166,25 +167,25 @@ namespace System { if (rtAdd != null) { rtAdd = rtAdd.GetParentDefinition(); - if (rtAdd != null) + if (rtAdd != null) return rtAdd.DeclaringType.GetEvent(ev.Name); } return null; } - private static bool InternalIsDefined (EventInfo element, Type attributeType, bool inherit) + private static bool InternalIsDefined(EventInfo element, Type attributeType, bool inherit) { Contract.Requires(element != null); // walk up the hierarchy chain if (element.IsDefined(attributeType, inherit)) return true; - + if (inherit) { AttributeUsageAttribute usage = InternalGetAttributeUsage(attributeType); - if (!usage.Inherited) + if (!usage.Inherited) return false; EventInfo baseEvent = GetParentDefinition(element); @@ -244,14 +245,14 @@ namespace System { // class inherits from and return the respective ParameterInfo attributes List<Type> disAllowMultiple = new List<Type>(); - Object [] objAttr; + Object[] objAttr; if (type == null) type = typeof(Attribute); - objAttr = param.GetCustomAttributes(type, false); - - for (int i =0;i < objAttr.Length;i++) + objAttr = param.GetCustomAttributes(type, false); + + for (int i = 0; i < objAttr.Length; i++) { Type objType = objAttr[i].GetType(); AttributeUsageAttribute attribUsage = InternalGetAttributeUsage(objType); @@ -260,26 +261,26 @@ namespace System { } // Get all the attributes that have Attribute as the base class - Attribute [] ret = null; - if (objAttr.Length == 0) - ret = CreateAttributeArrayHelper(type,0); - else + Attribute[] ret = null; + if (objAttr.Length == 0) + ret = CreateAttributeArrayHelper(type, 0); + else ret = (Attribute[])objAttr; - + if (param.Member.DeclaringType == null) // This is an interface so we are done. return ret; - - if (!inherit) + + if (!inherit) return ret; ParameterInfo baseParam = GetParentDefinition(param); while (baseParam != null) { - objAttr = baseParam.GetCustomAttributes(type, false); - + objAttr = baseParam.GetCustomAttributes(type, false); + int count = 0; - for (int i =0;i < objAttr.Length;i++) + for (int i = 0; i < objAttr.Length; i++) { Type objType = objAttr[i].GetType(); AttributeUsageAttribute attribUsage = InternalGetAttributeUsage(objType); @@ -295,10 +296,10 @@ namespace System { } // Get all the attributes that have Attribute as the base class - Attribute [] attributes = CreateAttributeArrayHelper(type,count); - + Attribute[] attributes = CreateAttributeArrayHelper(type, count); + count = 0; - for (int i =0;i < objAttr.Length;i++) + for (int i = 0; i < objAttr.Length; i++) { if (objAttr[i] != null) { @@ -306,18 +307,18 @@ namespace System { count++; } } - - Attribute [] temp = ret; - ret = CreateAttributeArrayHelper(type,temp.Length + count); - Array.Copy(temp,ret,temp.Length); - + + Attribute[] temp = ret; + ret = CreateAttributeArrayHelper(type, temp.Length + count); + Array.Copy(temp, ret, temp.Length); + int offset = temp.Length; - for (int i =0;i < attributes.Length;i++) + for (int i = 0; i < attributes.Length; i++) ret[offset + i] = attributes[i]; baseParam = GetParentDefinition(baseParam); - } + } return ret; } @@ -335,7 +336,7 @@ namespace System { if (param.IsDefined(type, false)) return true; - + if (param.Member.DeclaringType == null || !inherit) // This is an interface so we are done. return false; @@ -343,9 +344,9 @@ namespace System { while (baseParam != null) { - Object[] objAttr = baseParam.GetCustomAttributes(type, false); - - for (int i =0; i < objAttr.Length; i++) + Object[] objAttr = baseParam.GetCustomAttributes(type, false); + + for (int i = 0; i < objAttr.Length; i++) { Type objType = objAttr[i].GetType(); AttributeUsageAttribute attribUsage = InternalGetAttributeUsage(objType); @@ -355,7 +356,7 @@ namespace System { } baseParam = GetParentDefinition(baseParam); - } + } return false; } @@ -363,15 +364,15 @@ namespace System { #endregion #region Utility - private static void CopyToArrayList(List<Attribute> attributeList,Attribute[] attributes,Dictionary<Type, AttributeUsageAttribute> types) + private static void CopyToArrayList(List<Attribute> attributeList, Attribute[] attributes, Dictionary<Type, AttributeUsageAttribute> types) { - for (int i = 0; i < attributes.Length; i++) + for (int i = 0; i < attributes.Length; i++) { attributeList.Add(attributes[i]); Type attrType = attributes[i].GetType(); - if (!types.ContainsKey(attrType)) + if (!types.ContainsKey(attrType)) types[attrType] = InternalGetAttributeUsage(attrType); } } @@ -393,21 +394,21 @@ namespace System { return Array.Empty<Type>(); } - private static void AddAttributesToList(List<Attribute> attributeList, Attribute[] attributes, Dictionary<Type, AttributeUsageAttribute> types) + private static void AddAttributesToList(List<Attribute> attributeList, Attribute[] attributes, Dictionary<Type, AttributeUsageAttribute> types) { - for (int i = 0; i < attributes.Length; i++) + for (int i = 0; i < attributes.Length; i++) { Type attrType = attributes[i].GetType(); AttributeUsageAttribute usage = null; types.TryGetValue(attrType, out usage); - if (usage == null) + if (usage == null) { // the type has never been seen before if it's inheritable add it to the list usage = InternalGetAttributeUsage(attrType); types[attrType] = usage; - if (usage.Inherited) + if (usage.Inherited) attributeList.Add(attributes[i]); } else if (usage.Inherited && usage.AllowMultiple) @@ -421,7 +422,7 @@ namespace System { private static AttributeUsageAttribute InternalGetAttributeUsage(Type type) { // Check if the custom attributes is Inheritable - Object [] obj = type.GetCustomAttributes(typeof(AttributeUsageAttribute), false); + Object[] obj = type.GetCustomAttributes(typeof(AttributeUsageAttribute), false); if (obj.Length == 1) return (AttributeUsageAttribute)obj[0]; @@ -430,7 +431,7 @@ namespace System { return AttributeUsageAttribute.Default; throw new FormatException( - Environment.GetResourceString("Format_AttributeUsage", type)); + SR.Format(SR.Format_AttributeUsage, type)); } private static Attribute[] CreateAttributeArrayHelper(Type elementType, int elementCount) @@ -448,7 +449,7 @@ namespace System { { return GetCustomAttributes(element, type, true); } - + public static Attribute[] GetCustomAttributes(MemberInfo element, Type type, bool inherit) { if (element == null) @@ -456,17 +457,17 @@ namespace System { if (type == null) throw new ArgumentNullException(nameof(type)); - + if (!type.IsSubclassOf(typeof(Attribute)) && type != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); Contract.EndContractBlock(); switch (element.MemberType) { - case MemberTypes.Property: + case MemberTypes.Property: return InternalGetCustomAttributes((PropertyInfo)element, type, inherit); - case MemberTypes.Event: + case MemberTypes.Event: return InternalGetCustomAttributes((EventInfo)element, type, inherit); default: @@ -487,17 +488,17 @@ namespace System { switch (element.MemberType) { - case MemberTypes.Property: + case MemberTypes.Property: return InternalGetCustomAttributes((PropertyInfo)element, typeof(Attribute), inherit); - case MemberTypes.Event: + case MemberTypes.Event: return InternalGetCustomAttributes((EventInfo)element, typeof(Attribute), inherit); default: return element.GetCustomAttributes(typeof(Attribute), inherit) as Attribute[]; } } - + public static bool IsDefined(MemberInfo element, Type attributeType) { return IsDefined(element, attributeType, true); @@ -511,23 +512,22 @@ namespace System { if (attributeType == null) throw new ArgumentNullException(nameof(attributeType)); - + if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); Contract.EndContractBlock(); - switch(element.MemberType) + switch (element.MemberType) { - case MemberTypes.Property: + case MemberTypes.Property: return InternalIsDefined((PropertyInfo)element, attributeType, inherit); - case MemberTypes.Event: + case MemberTypes.Event: return InternalIsDefined((EventInfo)element, attributeType, inherit); default: return element.IsDefined(attributeType, inherit); } - } public static Attribute GetCustomAttribute(MemberInfo element, Type attributeType) @@ -545,7 +545,7 @@ namespace System { if (attrib.Length == 1) return attrib[0]; - throw new AmbiguousMatchException(Environment.GetResourceString("RFLCT.AmbigCust")); + throw new AmbiguousMatchException(SR.RFLCT_AmbigCust); } #endregion @@ -555,7 +555,7 @@ namespace System { { return GetCustomAttributes(element, true); } - + public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType) { return (Attribute[])GetCustomAttributes(element, attributeType, true); @@ -568,17 +568,17 @@ namespace System { if (attributeType == null) throw new ArgumentNullException(nameof(attributeType)); - + if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); if (element.Member == null) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidParameterInfo"), nameof(element)); + throw new ArgumentException(SR.Argument_InvalidParameterInfo, nameof(element)); Contract.EndContractBlock(); MemberInfo member = element.Member; - if (member.MemberType == MemberTypes.Method && inherit) + if (member.MemberType == MemberTypes.Method && inherit) return InternalParamGetCustomAttributes(element, attributeType, inherit) as Attribute[]; return element.GetCustomAttributes(attributeType, inherit) as Attribute[]; @@ -590,14 +590,14 @@ namespace System { throw new ArgumentNullException(nameof(element)); if (element.Member == null) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidParameterInfo"), nameof(element)); + throw new ArgumentException(SR.Argument_InvalidParameterInfo, nameof(element)); Contract.EndContractBlock(); MemberInfo member = element.Member; - if (member.MemberType == MemberTypes.Method && inherit) + if (member.MemberType == MemberTypes.Method && inherit) return InternalParamGetCustomAttributes(element, null, inherit) as Attribute[]; - + return element.GetCustomAttributes(typeof(Attribute), inherit) as Attribute[]; } @@ -614,14 +614,14 @@ namespace System { if (attributeType == null) throw new ArgumentNullException(nameof(attributeType)); - + if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); Contract.EndContractBlock(); MemberInfo member = element.Member; - switch(member.MemberType) + switch (member.MemberType) { case MemberTypes.Method: // We need to climb up the member hierarchy return InternalParamIsDefined(element, attributeType, inherit); @@ -632,9 +632,9 @@ namespace System { case MemberTypes.Property: return element.IsDefined(attributeType, false); - default: + default: Debug.Assert(false, "Invalid type for ParameterInfo member in Attribute class"); - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidParamInfo")); + throw new ArgumentException(SR.Argument_InvalidParamInfo); } } @@ -658,7 +658,7 @@ namespace System { if (attrib.Length == 1) return attrib[0]; - throw new AmbiguousMatchException(Environment.GetResourceString("RFLCT.AmbigCust")); + throw new AmbiguousMatchException(SR.RFLCT_AmbigCust); } #endregion @@ -666,7 +666,7 @@ namespace System { #region Module public static Attribute[] GetCustomAttributes(Module element, Type attributeType) { - return GetCustomAttributes (element, attributeType, true); + return GetCustomAttributes(element, attributeType, true); } public static Attribute[] GetCustomAttributes(Module element) @@ -692,7 +692,7 @@ namespace System { throw new ArgumentNullException(nameof(attributeType)); if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); Contract.EndContractBlock(); return (Attribute[])element.GetCustomAttributes(attributeType, inherit); @@ -713,10 +713,10 @@ namespace System { throw new ArgumentNullException(nameof(attributeType)); if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); Contract.EndContractBlock(); - return element.IsDefined(attributeType,false); + return element.IsDefined(attributeType, false); } public static Attribute GetCustomAttribute(Module element, Type attributeType) @@ -728,7 +728,7 @@ namespace System { { // Returns an Attribute of base class/inteface attributeType on the Module or null if none exists. // throws an AmbiguousMatchException if there are more than one defined. - Attribute[] attrib = GetCustomAttributes(element,attributeType,inherit); + Attribute[] attrib = GetCustomAttributes(element, attributeType, inherit); if (attrib == null || attrib.Length == 0) return null; @@ -736,7 +736,7 @@ namespace System { if (attrib.Length == 1) return attrib[0]; - throw new AmbiguousMatchException(Environment.GetResourceString("RFLCT.AmbigCust")); + throw new AmbiguousMatchException(SR.RFLCT_AmbigCust); } #endregion @@ -756,7 +756,7 @@ namespace System { throw new ArgumentNullException(nameof(attributeType)); if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); Contract.EndContractBlock(); return (Attribute[])element.GetCustomAttributes(attributeType, inherit); @@ -776,12 +776,12 @@ namespace System { return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit); } - public static bool IsDefined (Assembly element, Type attributeType) + public static bool IsDefined(Assembly element, Type attributeType) { - return IsDefined (element, attributeType, true); + return IsDefined(element, attributeType, true); } - public static bool IsDefined (Assembly element, Type attributeType, bool inherit) + public static bool IsDefined(Assembly element, Type attributeType, bool inherit) { // Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk if (element == null) @@ -791,7 +791,7 @@ namespace System { throw new ArgumentNullException(nameof(attributeType)); if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute)) - throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass")); + throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass); Contract.EndContractBlock(); return element.IsDefined(attributeType, false); @@ -799,14 +799,14 @@ namespace System { public static Attribute GetCustomAttribute(Assembly element, Type attributeType) { - return GetCustomAttribute (element, attributeType, true); + return GetCustomAttribute(element, attributeType, true); } public static Attribute GetCustomAttribute(Assembly element, Type attributeType, bool inherit) { // Returns an Attribute of base class/inteface attributeType on the Assembly or null if none exists. // throws an AmbiguousMatchException if there are more than one defined. - Attribute[] attrib = GetCustomAttributes(element,attributeType,inherit); + Attribute[] attrib = GetCustomAttributes(element, attributeType, inherit); if (attrib == null || attrib.Length == 0) return null; @@ -814,7 +814,7 @@ namespace System { if (attrib.Length == 1) return attrib[0]; - throw new AmbiguousMatchException(Environment.GetResourceString("RFLCT.AmbigCust")); + throw new AmbiguousMatchException(SR.RFLCT_AmbigCust); } #endregion @@ -945,7 +945,7 @@ namespace System { #region Public Virtual Members public virtual Object TypeId { get { return GetType(); } } - + public virtual bool Match(Object obj) { return Equals(obj); } #endregion |