Exemplo n.º 1
0
        //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());
        }
Exemplo n.º 3
0
 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);
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        /// <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);
        }
Exemplo n.º 9
0
        /// <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);
        }
Exemplo n.º 10
0
 /// <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)
                           )
                       );
     }
 }
Exemplo n.º 11
0
        /// <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);
        }
Exemplo n.º 14
0
 public XmlAttributes(System.Reflection.ICustomAttributeProvider provider)
 {
 }
Exemplo n.º 15
0
 public static bool HasAttribute(System.Reflection.ICustomAttributeProvider provider, System.Type attributeType, bool inherit)
 {
     return(HasAttribute(provider, attributeType.FullName, inherit));
 }
Exemplo n.º 16
0
 /// <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());
 }
Exemplo n.º 17
0
 public static System.Collections.Generic.List <object> GetAttributes(System.Reflection.ICustomAttributeProvider provider, System.Type attributeType, bool inherit)
 {
     return(GetAttributes(provider, attributeType.FullName, inherit));
 }
Exemplo n.º 18
0
 /// <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));
 }
Exemplo n.º 19
0
 /// <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)
 {
 }