Example #1
0
    static void ValidateHidden(ICustomAttributeProvider attributeProvider)
    {
        var customAttributes = attributeProvider.GetCustomAttributes(typeof(EditorBrowsableAttribute), false);
        var attribute        = (EditorBrowsableAttribute)customAttributes.First();

        Assert.Equal(EditorBrowsableState.Advanced, attribute.State);
    }
Example #2
0
        /// <summary>
        /// 获取自定义Attribute列表
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <param name="type">特性类型, instance is type。</param>
        /// <param name="inherit">如果为 true,则指定还在 element 的祖先中搜索自定义特性。</param>
        /// <returns></returns>
        public static System.Collections.IList GetCustomAttributes(
#if !net20
            this
#endif
            System.Reflection.ICustomAttributeProvider customAttributeProvider, System.Type type, bool inherit)
        {
            if (customAttributeProvider == null || type == null)
            {
                return(CreateList(type));
            }

            System.Collections.IList list;
            string key = string.Concat(GetKeyBefore(customAttributeProvider), "_", type.AssemblyQualifiedName);

            if (!_list_key.TryGetValue(key, out list))
            {
                ThreadHelper.Block(_list_key, () => {
                    if (!_list_key.TryGetValue(key, out list))
                    {
                        list = CreateList(type);
                        foreach (var item in customAttributeProvider.GetCustomAttributes(inherit))
                        {
                            if (item.GetType() == type || TypeExtensions.IsInheritFrom(item.GetType(), type))
                            {
                                list.Add(item);
                            }
                        }
                        _list_key.TryAdd(key, list);
                    }
                });
            }

            return(list);
        }
Example #3
0
        /// <summary>
        /// 检查是否定义此特性
        /// </summary>
        /// <typeparam name="T">识别instance is T</typeparam>
        /// <param name="customAttributeProvider"></param>
        /// <param name="inherit">如果为 true,则指定还在 element 的祖先中搜索自定义特性。</param>
        /// <returns></returns>
        public static bool IsDefined <T>(
#if !net20
            this
#endif
            System.Reflection.ICustomAttributeProvider customAttributeProvider, bool inherit) where T : System.Attribute
        {
            return(GetCustomAttribute <T>(customAttributeProvider, inherit) != null);
        }
Example #4
0
        /// <summary>
        /// 检查是否定义此特性
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <param name="type">特性类型, instance is type。</param>
        /// <param name="inherit">如果为 true,则指定还在 element 的祖先中搜索自定义特性。</param>
        /// <returns></returns>
        public static bool IsDefined(
#if !net20
            this
#endif
            System.Reflection.ICustomAttributeProvider customAttributeProvider, System.Type type, bool inherit)
        {
            return(GetCustomAttribute(customAttributeProvider, type, inherit) != null);
        }
Example #5
0
        /// <summary>
        /// 获取自定义Attribute列表(继承)
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <param name="type">特性类型, instance is type。</param>
        /// <returns></returns>
        public static System.Collections.IList GetCustomAttributes(
#if !net20
            this
#endif
            System.Reflection.ICustomAttributeProvider customAttributeProvider, System.Type type)
        {
            return(GetCustomAttributes(customAttributeProvider, type, true));
        }
Example #6
0
        /// <summary>
        /// 获取自定义Attribute列表
        /// </summary>
        /// <typeparam name="T">识别instance is T</typeparam>
        /// <param name="customAttributeProvider"></param>
        /// <param name="inherit">如果为 true,则指定还在 element 的祖先中搜索自定义特性。</param>
        /// <returns></returns>
        public static System.Collections.Generic.List <T> GetCustomAttributes <T>(
#if !net20
            this
#endif
            System.Reflection.ICustomAttributeProvider customAttributeProvider, bool inherit) where T : System.Attribute
        {
            return((System.Collections.Generic.List <T>)GetCustomAttributes(customAttributeProvider, typeof(T), inherit));
        }
Example #7
0
        /// <summary>
        /// 获取自定义Attribute中的第一个对象
        /// </summary>
        /// <typeparam name="T">识别instance is T</typeparam>
        /// <param name="customAttributeProvider"></param>
        /// <param name="inherit">如果为 true,则指定还在 element 的祖先中搜索自定义特性。</param>
        /// <returns></returns>
        public static T GetCustomAttribute <T>(
#if !net20
            this
#endif
            System.Reflection.ICustomAttributeProvider customAttributeProvider, bool inherit) where T : System.Attribute
        {
            return((T)GetCustomAttribute(customAttributeProvider, typeof(T), inherit));
        }
Example #8
0
 static ExportParameterKind GetExportKind(System.Reflection.ICustomAttributeProvider p)
 {
     foreach (ExportParameterAttribute a in p.GetCustomAttributes(typeof(ExportParameterAttribute), false))
     {
         return(a.Kind);
     }
     return(ExportParameterKind.Unspecified);
 }
Example #9
0
        /// <summary>
        /// 获取自定义Attribute中的第一个对象
        /// </summary>
        /// <param name="customAttributeProvider"></param>
        /// <param name="type">特性类型, instance is type。</param>
        /// <param name="inherit">如果为 true,则指定还在 element 的祖先中搜索自定义特性。</param>
        /// <returns></returns>
        public static Attribute GetCustomAttribute(
#if !net20
            this
#endif
            System.Reflection.ICustomAttributeProvider customAttributeProvider, System.Type type, bool inherit)
        {
            var list = GetCustomAttributes(customAttributeProvider, type, inherit);

            return(list.Count == 0 ? null : (Attribute)list[0]);
        }
        private static ExpectedStackAnalysisResultAttribute ExpectedStackAnalysisResultFor(
            ICustomAttributeProvider method)
        {
            foreach (Attribute attribute in method.GetCustomAttributes(false))
            {
                if (attribute.GetType() == typeof(ExpectedStackAnalysisResultAttribute))
                {
                    return((ExpectedStackAnalysisResultAttribute)attribute);
                }
            }

            return(new ExpectedStackAnalysisResultAttribute());
        }
Example #11
0
        private void MapCustomAttributes(SR.ICustomAttributeProvider provider, MC.ICustomAttributeProvider targetProvider)
        {
            var type = provider.GetType();

            // System.Reflection.Module.GetCustomAttributesData() not implemented in mono <= 3.4.0
            if (_is_running_mono && typeof(System.Reflection.Module).IsAssignableFrom(type))
            {
                return;
            }

            var method = type.GetMethod("GetCustomAttributesData");

            if (method == null)
            {
                throw new NotSupportedException("No method GetCustomAttributesData for type " + provider.GetType().FullName);
            }

            var custom_attributes_data = (IList <CustomAttributeData>)method.Invoke(provider, new object[0]);

            foreach (var custom_attribute_data in custom_attributes_data)
            {
                var custom_attribute = new CustomAttribute(CreateReference(custom_attribute_data.Constructor, null));

                foreach (var argument in custom_attribute_data.ConstructorArguments)
                {
                    custom_attribute.ConstructorArguments.Add(CustomAttributeArgumentFor(argument));
                }

                foreach (var named_argument in custom_attribute_data.NamedArguments)
                {
                    var argument = new MC.CustomAttributeNamedArgument(named_argument.MemberInfo.Name, CustomAttributeArgumentFor(named_argument.TypedValue));
                    if (named_argument.MemberInfo is PropertyInfo)
                    {
                        custom_attribute.Properties.Add(argument);
                    }
                    else if (named_argument.MemberInfo is FieldInfo)
                    {
                        custom_attribute.Fields.Add(argument);
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }

                targetProvider.CustomAttributes.Add(custom_attribute);
            }
        }
Example #12
0
 static object GetKeyBefore(System.Reflection.ICustomAttributeProvider customAttributeProvider)
 {
     {
         var value = customAttributeProvider as System.Type;
         if (value != null)
         {
             return(value.AssemblyQualifiedName);
         }
     }
     {
         var value = customAttributeProvider as System.Reflection.MemberInfo;
         if (value != null)
         {
             return(value.DeclaringType.AssemblyQualifiedName + "/" + value.Name);
         }
     }
     return(customAttributeProvider.GetHashCode());
 }
Example #13
0
        static StackObject *GetCustomAttributes_0(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 3);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.Boolean @inherit = ptr_of_this_method->Value == 1;

            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            System.Type @attributeType = (System.Type) typeof(System.Type).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 3);
            System.Reflection.ICustomAttributeProvider instance_of_this_method = (System.Reflection.ICustomAttributeProvider) typeof(System.Reflection.ICustomAttributeProvider).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = instance_of_this_method.GetCustomAttributes(@attributeType, @inherit);

            return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method));
        }
Example #14
0
        static StackObject *IsApplyAttr_0(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 3);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.Boolean bInherit = ptr_of_this_method->Value == 1;
            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            System.Type rType = (System.Type) typeof(System.Type).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);
            ptr_of_this_method = ILIntepreter.Minus(__esp, 3);
            System.Reflection.ICustomAttributeProvider rProvider = (System.Reflection.ICustomAttributeProvider) typeof(System.Reflection.ICustomAttributeProvider).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = Core.ICustomAttributeProviderExpand.IsApplyAttr(rProvider, rType, bInherit);

            __ret->ObjectType = ObjectTypes.Integer;
            __ret->Value      = result_of_this_method ? 1 : 0;
            return(__ret + 1);
        }
Example #15
0
        /// <summary>
        /// returns all instances of the given custom attribute on a given object
        /// </summary>
        /// <param name="objectToGetAttributeFrom"></param>
        /// <param name="customAttribute"></param>
        /// <returns></returns>
        public static Attribute GetCustomAttribute(object objectToGetAttributeFrom, Type customAttribute)
        {
            //first, see if the object implemented this interface to override standard behavior
            System.Reflection.ICustomAttributeProvider attribProvider = objectToGetAttributeFrom as System.Reflection.ICustomAttributeProvider;
            if (attribProvider == null)
            {
                //if not, get it from its type
                attribProvider = (System.Reflection.ICustomAttributeProvider)objectToGetAttributeFrom.GetType();
            }

            object[] attribs = attribProvider.GetCustomAttributes(customAttribute, true);
            if (attribs != null && attribs.Length > 0)
            {
                //NOTE: important that we'll always use the first one in collection.
                //Our implementation of ICustomAttributeProvider knows about that and
                //relies on this behavior
                return(attribs[0] as Attribute);
            }

            return(null);
        }
Example #16
0
    static void ValidateIsNotError(ICustomAttributeProvider attributeProvider)
    {
        var obsoleteAttribute = ReadAttribute(attributeProvider);

        Assert.False(obsoleteAttribute.IsError);
    }
		private static ExpectedStackAnalysisResultAttribute ExpectedStackAnalysisResultFor(ICustomAttributeProvider method)
		{
			foreach(Attribute attribute in method.GetCustomAttributes(false))
			{
				if (attribute.GetType() == typeof(ExpectedStackAnalysisResultAttribute))
				{
					return (ExpectedStackAnalysisResultAttribute) attribute;
				}
			}

			return new ExpectedStackAnalysisResultAttribute();
		}
Example #18
0
    static ObsoleteAttribute ReadAttribute(ICustomAttributeProvider attributeProvider)
    {
        var customAttributes = attributeProvider.GetCustomAttributes(typeof(ObsoleteAttribute), false);

        return((ObsoleteAttribute)customAttributes.First());
    }
Example #19
0
    static void ValidateMessage(ICustomAttributeProvider attributeProvider)
    {
        var obsoleteAttribute = ReadAttribute(attributeProvider);

        Assert.Equal("Custom message. Use `NewThing` instead. Will be treated as an error from version 2.0.0. Will be removed in version 4.0.0.", obsoleteAttribute.Message);
    }
Example #20
0
 static void ValidateIsNotError(ICustomAttributeProvider attributeProvider)
 {
     var obsoleteAttribute = ReadAttribute(attributeProvider);
     Assert.IsFalse(obsoleteAttribute.IsError);
 }
Example #21
0
 static void ValidateHidden(ICustomAttributeProvider attributeProvider)
 {
     var customAttributes = attributeProvider.GetCustomAttributes(typeof(EditorBrowsableAttribute), false);
     var attribute = (EditorBrowsableAttribute) customAttributes.First();
     Assert.AreEqual(EditorBrowsableState.Advanced, attribute.State);
 }
Example #22
0
 static ObsoleteAttribute ReadAttribute(ICustomAttributeProvider attributeProvider)
 {
     var customAttributes = attributeProvider.GetCustomAttributes(typeof(ObsoleteAttribute), false);
     return (ObsoleteAttribute) customAttributes.First();
 }
Example #23
0
 static void ValidateMessage(ICustomAttributeProvider attributeProvider)
 {
     var obsoleteAttribute = ReadAttribute(attributeProvider);
     Assert.AreEqual("Custom message. Use `NewThing` instead. Will be treated as an error from version 2.0.0. Will be removed in version 4.0.0.", obsoleteAttribute.Message);
 }