Ejemplo n.º 1
0
        public static IList <System.Reflection.CustomAttributeData> ToList(List <CustomAttributeData> customAttributeDataList)
        {
            var returnList                 = new List <System.Reflection.CustomAttributeData>();
            var customAttributeType        = typeof(System.Reflection.CustomAttributeData);
            var customAttributeTypeName    = customAttributeType.FullName;
            var customAttributeConstructor = customAttributeType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(Attribute) }, null);
            var ctorArgsField              = customAttributeType.GetField("m_typedCtorArgs", BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic);
            var namedArgsField             = customAttributeType.GetField("m_namedArgs", BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic);

            Debug.Assert(customAttributeConstructor != null);
            DebugUtils.ThrowIf(customAttributeConstructor == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get constructor for type '{0}'", customAttributeTypeName)));

            DebugUtils.ThrowIf(ctorArgsField == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get field m_typedCtorArgs for type '{0}'", customAttributeTypeName)));
            DebugUtils.ThrowIf(namedArgsField == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get field m_namedArgs for type '{0}'", customAttributeTypeName)));

            foreach (var data in customAttributeDataList)
            {
                System.Reflection.CustomAttributeData attributeData;
                Attribute       attribute = null;
                ConstructorInfo constructor;
                var             attributeType = Type.GetType(data.AttributeType);
                var             parms         = new List <object>();
                var             types         = new List <Type>();
                var             ctorArgs      = new List <System.Reflection.CustomAttributeTypedArgument>();
                var             namedArgs     = new List <System.Reflection.CustomAttributeNamedArgument>();

                DebugUtils.ThrowIf(attributeType == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get type for '{0}'", data.AttributeType)));

                if (data.ConstructorArguments.Count > 0)
                {
                    foreach (var arg in data.ConstructorArguments)
                    {
                        var type = Type.GetType(arg.ArgumentType);

                        DebugUtils.ThrowIf(type == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get type for '{0}'", arg.ArgumentType)));

                        types.Add(type);
                    }

                    constructor = attributeType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, types.ToArray(), null);

                    DebugUtils.ThrowIf(constructor == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get constructor for '{0}'", attributeType.FullName)));

                    foreach (var arg in data.ConstructorArguments)
                    {
                        var type    = Type.GetType(arg.ArgumentType);
                        var value   = arg.Value.Convert(type);
                        var ctorArg = new System.Reflection.CustomAttributeTypedArgument(type, value);

                        ctorArgs.Add(ctorArg);
                        parms.Add(value);
                    }

                    attribute = (Attribute)constructor.Invoke(parms.ToArray());
                }
                else
                {
                    constructor = attributeType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, types.ToArray(), null);

                    DebugUtils.ThrowIf(constructor == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get constructor for '{0}'", attributeType.FullName)));

                    attribute = (Attribute)constructor.Invoke(parms.ToArray());
                }

                if (data.NamedArguments.Count > 0)
                {
                    types = new List <Type>();

                    foreach (var arg in data.NamedArguments)
                    {
                        var type  = Type.GetType(arg.ArgumentType);
                        var value = arg.Value.Convert(type);
                        System.Reflection.CustomAttributeNamedArgument namedArg;

                        if (arg.IsField)
                        {
                            var fieldInfo = attributeType.GetField(arg.MemberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty);

                            DebugUtils.ThrowIf(fieldInfo == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get field '{0}' for '{0}'", arg.MemberName, attributeType.FullName)));
                            namedArg = new System.Reflection.CustomAttributeNamedArgument(fieldInfo, value);

                            fieldInfo.SetValue(attribute, value);
                        }
                        else
                        {
                            var propertyInfo = attributeType.GetProperty(arg.MemberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty);

                            DebugUtils.ThrowIf(propertyInfo == null, () => new NullReferenceException(string.Format("Utils.CustomAttributeData.ToList(System.Reflection.CustomAttributeData list) could not get property '{0}' for '{0}'", arg.MemberName, attributeType.FullName)));
                            namedArg = new System.Reflection.CustomAttributeNamedArgument(propertyInfo, value);

                            propertyInfo.GetSetMethod().Invoke(attribute, new object[] { value });
                        }

                        namedArgs.Add(namedArg);
                    }
                }

                attributeData = (System.Reflection.CustomAttributeData)customAttributeConstructor.Invoke(new object[] { attribute });

                ctorArgsField.SetValue(attributeData, ctorArgs);
                namedArgsField.SetValue(attributeData, namedArgs);

                returnList.Add(attributeData);
            }

            return(returnList);
        }