//http://www.codeproject.com/Tips/72637/Get-CustomAttributes-the-easy-way.aspx /// <summary>Returns first custom attribute of type T in the inheritance chain</summary> public static T GetCustomAttribute <T>(this System.Reflection.ICustomAttributeProvider provider /*, bool inherited = false*/) where T : Attribute { bool inherited = false; return(provider.GetCustomAttributes <T>(inherited).FirstOrDefault()); }
private string GetAlias(System.Reflection.ICustomAttributeProvider methodInfo) { var aliasAttrs = methodInfo.GetCustomAttributes(typeof(AliasAttribute), false); if (aliasAttrs.Length == 0) { return(string.Empty); } return((aliasAttrs[0] as AliasAttribute)?.text.ToLower()); }
public static bool HasAttribute(System.Reflection.ICustomAttributeProvider provider, string attributeName, bool inherit) { foreach (var attribute in provider.GetCustomAttributes(inherit)) { if (IsA(attribute.GetType(), attributeName)) { return(true); } } return(false); }
private bool GetFastLink(System.Reflection.ICustomAttributeProvider methodInfo, out string text) { text = string.Empty; var aliasAttrs = methodInfo.GetCustomAttributes(typeof(FastLinkAttribute), false); if (aliasAttrs.Length == 0) { return(false); } text = (aliasAttrs[0] as FastLinkAttribute)?.text.ToLower(); return(true); }
/// <summary> /// Tests whether the object provides the expected attribute. /// </summary> /// <param name="actual">A Type, MethodInfo, or other ICustomAttributeProvider</param> /// <returns>True if the expected attribute is present, otherwise false</returns> public override bool Matches(object actual) { this.actual = actual; System.Reflection.ICustomAttributeProvider attrProvider = actual as System.Reflection.ICustomAttributeProvider; if (attrProvider == null) { throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual"); } return(attrProvider.GetCustomAttributes(expectedType, true).Length > 0); }
public static System.Collections.Generic.List <object> GetAttributes(System.Reflection.ICustomAttributeProvider provider, string attributeName, bool inherit) { var attributes = new System.Collections.Generic.List <object>(); foreach (var attribute in provider.GetCustomAttributes(inherit)) { if (IsA(attribute.GetType(), attributeName)) { attributes.Add(attribute); } } return(attributes); }
public static System.Collections.Generic.List <string> GetCategories(System.Reflection.ICustomAttributeProvider provider) { var categories = new System.Collections.Generic.List <string>(); foreach (var category in Reflection.GetAttributes(provider, typeof(NUnit.Framework.CategoryAttribute), false)) { var categoryName = Reflection.GetProperty(category, "Name"); if (categoryName != null) { categories.Add(categoryName as string); } } return(categories); }
/// <summary> /// Tests whether the object provides the expected attribute. /// </summary> /// <param name="actual">A Type, MethodInfo, or other ICustomAttributeProvider</param> /// <returns>True if the expected attribute is present, otherwise false</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { System.Reflection.ICustomAttributeProvider attrProvider = actual as System.Reflection.ICustomAttributeProvider; if (attrProvider == null) { throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual"); } ConstraintResult result = new ConstraintResult(this, actual); result.Status = attrProvider.GetCustomAttributes(expectedType, true).Length > 0 ? ConstraintStatus.Success : ConstraintStatus.Failure; return(result); }
/// <summary> /// 添加。 /// </summary> /// <param name="customAttributeProvider">可承载特性的对象:Type或MethodInfo,为null直接返回0。</param> /// <returns>返回成功添加的数量。</returns> public int AddRange(System.Reflection.ICustomAttributeProvider customAttributeProvider) { if (customAttributeProvider == null) { return(0); } int count = 0; foreach (var item in customAttributeProvider.GetCustomAttributes <CloudActionFilterAttribute>(true)) { if (Add(item)) { count++; } } return(count); }
/// <summary> /// 加载。 /// </summary> /// <param name="list">用于存储的列表。</param> /// <param name="attributeProvider">特性提供者。</param> public static void Load(ParameterInfoList list, System.Reflection.ICustomAttributeProvider attributeProvider) { if (list == null || attributeProvider == null) { return; } foreach (var attribute in AttributeExtensions.GetCustomAttributes <CloudArgsAttribute>(attributeProvider, true)) { if (attribute.Type == null) { continue; } list.AddRange(PropertyParameterInfo.As( FastWrapper.GetProperties(attribute.Type, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, true, true) ) ); } }
/// <summary> /// Determines whether the Type or other provider has the /// expected attribute and if its value matches the /// additional constraint specified. /// </summary> public override bool Matches(object actual) { this.actual = actual; System.Reflection.ICustomAttributeProvider attrProvider = actual as System.Reflection.ICustomAttributeProvider; if (attrProvider == null) { throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual"); } Attribute[] attrs = (Attribute[])attrProvider.GetCustomAttributes(expectedType, true); if (attrs.Length == 0) { throw new ArgumentException(string.Format("Attribute {0} was not found", expectedType), "actual"); } attrFound = attrs[0]; return(baseConstraint.Matches(attrFound)); }
/// <summary> /// Determines whether the Type or other provider has the /// expected attribute and if its value matches the /// additional constraint specified. /// </summary> public override ConstraintResult ApplyTo <TActual>(TActual actual) { System.Reflection.ICustomAttributeProvider attrProvider = actual as System.Reflection.ICustomAttributeProvider; // TODO: Use Error Result for these rather than throwing an exception if (attrProvider == null) { throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual"); } Attribute[] attrs = (Attribute[])attrProvider.GetCustomAttributes(expectedType, true); if (attrs.Length == 0) { throw new ArgumentException(string.Format("Attribute {0} was not found", expectedType), "actual"); } attrFound = attrs[0]; return(baseConstraint.ApplyTo(attrFound)); }
private string GetHelpString(string callStr, System.Reflection.ICustomAttributeProvider type) { var length = callStr.Length; var str = string.Empty; var attrs = type.GetCustomAttributes(typeof(HelpAttribute), false); if (attrs.Length > 0) { str += this.GetSpace(4) + "<color=#999>" + ((HelpAttribute)attrs[0]).text + "</color>"; } var attrsAlias = type.GetCustomAttributes(typeof(AliasAttribute), false); if (attrsAlias.Length > 0) { str += "\n" + this.GetSpace(length + 4) + "Alias: <color=#3af>" + ((AliasAttribute)attrsAlias[0]).text + "</color>"; } return(str); }
public XmlAttributes(System.Reflection.ICustomAttributeProvider provider) { }
public static bool HasAttribute(System.Reflection.ICustomAttributeProvider provider, System.Type attributeType, bool inherit) { return(HasAttribute(provider, attributeType.FullName, inherit)); }
/// <summary>Returns all custom attributes of type T in the inheritance chain</summary> public static List <T> GetCustomAttributes <T>(this System.Reflection.ICustomAttributeProvider provider, bool inherited /* = false*/) where T : Attribute { return(provider.GetCustomAttributes(typeof(T), inherited).Cast <T>().ToList()); }
public static System.Collections.Generic.List <object> GetAttributes(System.Reflection.ICustomAttributeProvider provider, System.Type attributeType, bool inherit) { return(GetAttributes(provider, attributeType.FullName, inherit)); }
/// <summary> /// Recupera osm atributos do tipo especificado. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="attributeProvider"></param> /// <returns></returns> public static T[] GetAttributes <T>(this System.Reflection.ICustomAttributeProvider attributeProvider) where T : class { return((T[])attributeProvider.GetCustomAttributes(typeof(T), false)); }
/// <summary> /// Recupera o primeiro atributo do tipo informado. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="attributeProvider"></param> /// <returns></returns> public static T GetFirstAttribute <T>(this System.Reflection.ICustomAttributeProvider attributeProvider) where T : class { return(attributeProvider.GetAttributes <T>().FirstOrDefault <T>()); }
// Methods public void IncludeTypes(System.Reflection.ICustomAttributeProvider provider) { }