public static bool IsMinValue(CustomAttributeTypedArgument argument)
        {
            if (argument.ArgumentType == typeof(int))
                return int.MinValue == (int)argument.Value;

            return Math.Abs(double.MinValue - (double)argument.Value) < 1;
        }
 public static object Format(CustomAttributeTypedArgument argument)
 {
     var us = new CultureInfo("en-US");
     return argument.ArgumentType == typeof (int)
         ? argument.Value
         : Convert.ToDouble(argument.Value).ToString("F", us);
 }
 // This constructor is the one used by .Net Native as the current metadata format only contains the name and the "isField" value,
 // not the actual member. To keep .Net Native running as before, we'll use the name and isField as the principal data and 
 // construct the MemberInfo on demand.
 internal CustomAttributeNamedArgument(Type attributeType, string memberName, bool isField, CustomAttributeTypedArgument typedValue)
 {
     IsField = isField;
     MemberName = memberName;
     TypedValue = typedValue;
     _attributeType = attributeType;
     _lazyMemberInfo = null;
 }
 public CustomAttributeNamedArgument(System.Reflection.MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
 {
     if (memberInfo == null)
     {
         throw new ArgumentNullException("memberInfo");
     }
     this.m_memberInfo = memberInfo;
     this.m_value = typedArgument;
 }
        CustomAttributeNamedArgument(MemberInfo memberInfo, object value)
        {
            if (memberInfo == null)
            {
                throw new ArgumentNullException(nameof(memberInfo));
            }

            this.memberInfo    = memberInfo;
            this.typedArgument = (CustomAttributeTypedArgument)value;
        }
Ejemplo n.º 6
0
        private void Init(DllImportAttribute dllImport)
        {
            Type type = typeof(DllImportAttribute);

            this.m_ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
            CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(dllImport.Value) };
            this.m_typedCtorArgs = Array.AsReadOnly <CustomAttributeTypedArgument>(array);
            CustomAttributeNamedArgument[] argumentArray2 = new CustomAttributeNamedArgument[] { new CustomAttributeNamedArgument(type.GetField("EntryPoint"), dllImport.EntryPoint), new CustomAttributeNamedArgument(type.GetField("CharSet"), dllImport.CharSet), new CustomAttributeNamedArgument(type.GetField("ExactSpelling"), dllImport.ExactSpelling), new CustomAttributeNamedArgument(type.GetField("SetLastError"), dllImport.SetLastError), new CustomAttributeNamedArgument(type.GetField("PreserveSig"), dllImport.PreserveSig), new CustomAttributeNamedArgument(type.GetField("CallingConvention"), dllImport.CallingConvention), new CustomAttributeNamedArgument(type.GetField("BestFitMapping"), dllImport.BestFitMapping), new CustomAttributeNamedArgument(type.GetField("ThrowOnUnmappableChar"), dllImport.ThrowOnUnmappableChar) };
            this.m_namedArgs = Array.AsReadOnly <CustomAttributeNamedArgument>(argumentArray2);
        }
Ejemplo n.º 7
0
        /// <returns>true if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, false.</returns>
        /// <param name="obj">Another object to compare to. </param>
        public override bool Equals(object obj)
        {
            if (!(obj is CustomAttributeTypedArgument))
            {
                return(false);
            }
            CustomAttributeTypedArgument customAttributeTypedArgument = (CustomAttributeTypedArgument)obj;

            return((customAttributeTypedArgument.argumentType != this.argumentType || this.value == null) ? (customAttributeTypedArgument.value == null) : this.value.Equals(customAttributeTypedArgument.value));
        }
        public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
        {
            if (memberInfo == null)
            {
                throw new ArgumentNullException(nameof(memberInfo));
            }

            this.memberInfo    = memberInfo;
            this.typedArgument = typedArgument;
        }
Ejemplo n.º 9
0
        public override bool Equals(object obj)
        {
            if (!(obj is CustomAttributeTypedArgument))
            {
                return(false);
            }
            CustomAttributeTypedArgument other = (CustomAttributeTypedArgument)obj;

            return(other.argumentType == argumentType &&
                   value != null?value.Equals(other.value) : (object)other.value == null);
        }
Ejemplo n.º 10
0
        public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
        {
            if (memberInfo == null)
                throw new ArgumentNullException(nameof(memberInfo));

            _lazyMemberInfo = memberInfo;
            _attributeType = memberInfo.DeclaringType;
            TypedValue = typedArgument;
            IsField = memberInfo is FieldInfo;  // For compat with the desktop, there is no validation that a non-field member is a PropertyInfo.
            MemberName = memberInfo.Name;
        }
Ejemplo n.º 11
0
        private void Init(TypeForwardedToAttribute forwardedTo)
        {
            Type type = typeof(TypeForwardedToAttribute);

            Type[] types = new Type[] { typeof(Type) };
            this.m_ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, types, null);
            CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(Type), forwardedTo.Destination) };
            this.m_typedCtorArgs = Array.AsReadOnly <CustomAttributeTypedArgument>(array);
            CustomAttributeNamedArgument[] argumentArray2 = new CustomAttributeNamedArgument[0];
            this.m_namedArgs = Array.AsReadOnly <CustomAttributeNamedArgument>(argumentArray2);
        }
        public CustomAttributeNamedArgument(Type attributeType, String memberName, bool isField, CustomAttributeTypedArgument typedValue)
        {
            if (attributeType == null)
                throw new ArgumentNullException("attributeType");

            if (memberName == null)
                throw new ArgumentNullException("memberName");

            _memberName = memberName;
            _isField = isField;
            _typedValue = typedValue;
        }
Ejemplo n.º 13
0
        internal CustomAttributeData[]? GetPseudoCustomAttributesData()
        {
            int count = 0;

            if (IsNotSerialized)
            {
                count++;
            }

            if (DeclaringType !.IsExplicitLayout)
            {
                count++;
            }

            MarshalAsAttribute marshalAs = get_marshal_info();

            if (marshalAs != null)
            {
                count++;
            }

            if (count == 0)
            {
                return(null);
            }
            CustomAttributeData[] attrsData = new CustomAttributeData[count];
            count = 0;

            if (IsNotSerialized)
            {
                attrsData[count++] = new RuntimeCustomAttributeData((typeof(NonSerializedAttribute)).GetConstructor(Type.EmptyTypes) !);
            }
            if (DeclaringType.IsExplicitLayout)
            {
                var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(int), GetFieldOffset()) };
                attrsData[count++] = new RuntimeCustomAttributeData(
                    (typeof(FieldOffsetAttribute)).GetConstructor(new[] { typeof(int) }) !,
                    ctorArgs,
                    Array.Empty <CustomAttributeNamedArgument>());
            }

            if (marshalAs != null)
            {
                var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(UnmanagedType), marshalAs.Value) };
                attrsData[count++] = new RuntimeCustomAttributeData(
                    (typeof(MarshalAsAttribute)).GetConstructor(new[] { typeof(UnmanagedType) }) !,
                    ctorArgs,
                    Array.Empty <CustomAttributeNamedArgument>());//FIXME Get named params
            }

            return(attrsData);
        }
Ejemplo n.º 14
0
        private void Init(MarshalAsAttribute marshalAs)
        {
            Type type = typeof(MarshalAsAttribute);

            this.m_ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
            CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(marshalAs.Value) };
            this.m_typedCtorArgs = Array.AsReadOnly <CustomAttributeTypedArgument>(array);
            int num = 3;

            if (marshalAs.MarshalType != null)
            {
                num++;
            }
            if (marshalAs.MarshalTypeRef != null)
            {
                num++;
            }
            if (marshalAs.MarshalCookie != null)
            {
                num++;
            }
            num++;
            num++;
            if (marshalAs.SafeArrayUserDefinedSubType != null)
            {
                num++;
            }
            CustomAttributeNamedArgument[] argumentArray = new CustomAttributeNamedArgument[num];
            num = 0;
            argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("ArraySubType"), marshalAs.ArraySubType);
            argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SizeParamIndex"), marshalAs.SizeParamIndex);
            argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SizeConst"), marshalAs.SizeConst);
            argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("IidParameterIndex"), marshalAs.IidParameterIndex);
            argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SafeArraySubType"), marshalAs.SafeArraySubType);
            if (marshalAs.MarshalType != null)
            {
                argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("MarshalType"), marshalAs.MarshalType);
            }
            if (marshalAs.MarshalTypeRef != null)
            {
                argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("MarshalTypeRef"), marshalAs.MarshalTypeRef);
            }
            if (marshalAs.MarshalCookie != null)
            {
                argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("MarshalCookie"), marshalAs.MarshalCookie);
            }
            if (marshalAs.SafeArrayUserDefinedSubType != null)
            {
                argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SafeArrayUserDefinedSubType"), marshalAs.SafeArrayUserDefinedSubType);
            }
            this.m_namedArgs = Array.AsReadOnly <CustomAttributeNamedArgument>(argumentArray);
        }
Ejemplo n.º 15
0
        private object GetArgumentValue(CustomAttributeTypedArgument argument)
        {
            var value = argument.GetValue();

              var valueAsTypeArray = value as Type[];
              if (valueAsTypeArray != null)
            return valueAsTypeArray.Select(GetCommonType).ToArray();

              if (value is Type)
            return ((Type) value).ToCommon();

              return value;
        }
        public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
        {
            if (memberInfo == null)
            {
                throw new ArgumentNullException(nameof(memberInfo));
            }

            _lazyMemberInfo = memberInfo;
            _attributeType  = memberInfo.DeclaringType;
            TypedValue      = typedArgument;
            IsField         = memberInfo is FieldInfo; // For compat with the desktop, there is no validation that a non-field member is a PropertyInfo.
            MemberName      = memberInfo.Name;
        }
Ejemplo n.º 17
0
        internal CustomAttributeData[] GetPseudoCustomAttributesData()
        {
            int count = 0;

            if (IsIn)
            {
                count++;
            }
            if (IsOut)
            {
                count++;
            }
            if (IsOptional)
            {
                count++;
            }
            if (marshalAs != null)
            {
                count++;
            }

            if (count == 0)
            {
                return(null);
            }
            CustomAttributeData[] attrsData = new CustomAttributeData [count];
            count = 0;

            if (IsIn)
            {
                attrsData [count++] = new CustomAttributeData((typeof(InAttribute)).GetConstructor(Type.EmptyTypes));
            }
            if (IsOut)
            {
                attrsData [count++] = new CustomAttributeData((typeof(OutAttribute)).GetConstructor(Type.EmptyTypes));
            }
            if (IsOptional)
            {
                attrsData [count++] = new CustomAttributeData((typeof(OptionalAttribute)).GetConstructor(Type.EmptyTypes));
            }
            if (marshalAs != null)
            {
                var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(UnmanagedType), marshalAs.Value) };
                attrsData [count++] = new CustomAttributeData(
                    (typeof(MarshalAsAttribute)).GetConstructor(new[] { typeof(UnmanagedType) }),
                    ctorArgs,
                    Array.Empty <CustomAttributeNamedArgument> ());                   //FIXME Get named params
            }

            return(attrsData);
        }
        public CustomAttributeNamedArgument(MemberInfo memberInfo, object?value)
        {
            ArgumentNullException.ThrowIfNull(memberInfo);

            Type type = memberInfo switch
            {
                FieldInfo field => field.FieldType,
                PropertyInfo property => property.PropertyType,
                 _ => throw new ArgumentException(SR.Argument_InvalidMemberForNamedArgument)
            };

            _memberInfo = memberInfo;
            _value      = new CustomAttributeTypedArgument(type, value);
        }
Ejemplo n.º 19
0
        public CustomAttributeNamedArgument(Type attributeType, String memberName, bool isField, CustomAttributeTypedArgument typedValue)
        {
            if (attributeType == null)
            {
                throw new ArgumentNullException("attributeType");
            }

            if (memberName == null)
            {
                throw new ArgumentNullException("memberName");
            }

            _memberName = memberName;
            _isField    = isField;
            _typedValue = typedValue;
        }
Ejemplo n.º 20
0
		internal CustomAttributeTypedArgument (Type argumentType, object value)
		{
			this.argumentType = argumentType;
			this.value = value;

			// MS seems to convert arrays into a ReadOnlyCollection
			if (value is Array) {
				Array a = (Array)value;

				Type etype = a.GetType ().GetElementType ();
				CustomAttributeTypedArgument[] new_value = new CustomAttributeTypedArgument [a.GetLength (0)];
				for (int i = 0; i < new_value.Length; ++i)
					new_value [i] = new CustomAttributeTypedArgument (etype, a.GetValue (i));
				this.value = new ReadOnlyCollection <CustomAttributeTypedArgument> (new_value);
			}
		}
Ejemplo n.º 21
0
 internal CustomAttributeTypedArgument(Type argumentType, object value)
 {
     this.argumentType = argumentType;
     this.value        = value;
     if (value is Array)
     {
         Array array       = (Array)value;
         Type  elementType = array.GetType().GetElementType();
         CustomAttributeTypedArgument[] array2 = new CustomAttributeTypedArgument[array.GetLength(0)];
         for (int i = 0; i < array2.Length; i++)
         {
             array2[i] = new CustomAttributeTypedArgument(elementType, array.GetValue(i));
         }
         this.value = new ReadOnlyCollection <CustomAttributeTypedArgument>(array2);
     }
 }
Ejemplo n.º 22
0
        internal CustomAttributeTypedArgument(Type argumentType, object value)
        {
            this.argumentType = argumentType;
            this.value        = value;

            // MS seems to convert arrays into a ReadOnlyCollection
            if (value is Array)
            {
                Array a = (Array)value;

                Type etype = a.GetType().GetElementType();
                CustomAttributeTypedArgument[] new_value = new CustomAttributeTypedArgument [a.GetLength(0)];
                for (int i = 0; i < new_value.Length; ++i)
                {
                    new_value [i] = new CustomAttributeTypedArgument(etype, a.GetValue(i));
                }
                this.value = new ReadOnlyCollection <CustomAttributeTypedArgument> (new_value);
            }
        }
Ejemplo n.º 23
0
        internal CustomAttributeData[] GetPseudoCustomAttributesData()
        {
            int count = 0;

            /* MS.NET doesn't report MethodImplAttribute */

            MonoMethodInfo info = MonoMethodInfo.GetMethodInfo(mhandle);

            if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
            {
                count++;
            }
            if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
            {
                count++;
            }

            if (count == 0)
            {
                return(null);
            }
            CustomAttributeData[] attrsData = new CustomAttributeData [count];
            count = 0;

            if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
            {
                attrsData [count++] = new CustomAttributeData((typeof(PreserveSigAttribute)).GetConstructor(Type.EmptyTypes));
            }
            if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
            {
                this.GetPInvoke(out PInvokeAttributes flags, out string entryPoint, out string dllName);
                var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(string), dllName) };
                attrsData [count++] = new CustomAttributeData(
                    (typeof(FieldOffsetAttribute)).GetConstructor(new[] { typeof(string) }),
                    ctorArgs,
                    EmptyArray <CustomAttributeNamedArgument> .Value);                   //FIXME Get named params
            }

            return(attrsData);
        }
Ejemplo n.º 24
0
        public CustomAttributeNamedArgument(MemberInfo memberInfo, object value)
        {
            if (memberInfo == null)
                throw new ArgumentNullException(nameof(memberInfo));

            Type type = null;
            FieldInfo field = memberInfo as FieldInfo;
            PropertyInfo property = memberInfo as PropertyInfo;

            if (field != null)
                type = field.FieldType;
            else if (property != null)
                type = property.PropertyType;
            else
                throw new ArgumentException(SR.Argument_InvalidMemberForNamedArgument);

            _lazyMemberInfo = memberInfo;
            _attributeType = memberInfo.DeclaringType;
            TypedValue = new CustomAttributeTypedArgument(type, value);
            IsField = field != null;
            MemberName = memberInfo.Name;
        }
Ejemplo n.º 25
0
        public CustomAttributeNamedArgument(MemberInfo memberInfo, object value)
        {
            if (memberInfo == null)
            {
                throw new ArgumentNullException(nameof(memberInfo));
            }

            Type         type     = null;
            FieldInfo    field    = memberInfo as FieldInfo;
            PropertyInfo property = memberInfo as PropertyInfo;

            if (field != null)
            {
                type = field.FieldType;
            }
            else if (property != null)
            {
                type = property.PropertyType;
            }
            else
            {
                throw new ArgumentException(SR.Argument_InvalidMemberForNamedArgument);
            }

            _lazyMemberInfo = memberInfo;
            _attributeType  = memberInfo.DeclaringType;
#if MONO
            // Mono runtime "create_cattr_named_arg" method passes value wrapped into CustomAttributeTypedArgument object
            // but CoreFX expects just value.
            if (value is CustomAttributeTypedArgument typedArument)
            {
                TypedValue = typedArument;
            }
            else
#endif
            TypedValue = new CustomAttributeTypedArgument(type, value);
            IsField    = field != null;
            MemberName = memberInfo.Name;
        }
        public CustomAttributeTypedArgument(Type argumentType, object value)
        {
            // value can be null.
            if (argumentType == null)
            {
                throw new ArgumentNullException(nameof(argumentType));
            }

            Value        = (value == null) ? null : CanonicalizeValue(value);
            ArgumentType = argumentType;
#if MONO
            if (value is Array a)
            {
                Type etype = a.GetType().GetElementType();
                CustomAttributeTypedArgument[] new_value = new CustomAttributeTypedArgument[a.GetLength(0)];
                for (int i = 0; i < new_value.Length; ++i)
                {
                    new_value[i] = new CustomAttributeTypedArgument(etype, a.GetValue(i));
                }
                Value = new System.Collections.ObjectModel.ReadOnlyCollection <CustomAttributeTypedArgument>(new_value);
            }
#endif
        }
 public CustomAttributeNamedArgument(System.Reflection.MemberInfo memberInfo, object value)
 {
     if (memberInfo == null)
     {
         throw new ArgumentNullException("memberInfo");
     }
     Type argumentType = null;
     FieldInfo info = memberInfo as FieldInfo;
     PropertyInfo info2 = memberInfo as PropertyInfo;
     if (info != null)
     {
         argumentType = info.FieldType;
     }
     else
     {
         if (info2 == null)
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_InvalidMemberForNamedArgument"));
         }
         argumentType = info2.PropertyType;
     }
     this.m_memberInfo = memberInfo;
     this.m_value = new CustomAttributeTypedArgument(argumentType, value);
 }
Ejemplo n.º 28
0
        CustomAttributeTypedArgument(Type argumentType, object value)
        {
            if (argumentType == null)
            {
                throw new ArgumentNullException("argumentType");
            }

            this.argumentType = argumentType;
            this.value        = (value == null) ? null : CanonicalizeValue(value);

            // MS seems to convert arrays into a ReadOnlyCollection
            if (value is Array)
            {
                Array a = (Array)value;

                Type etype = a.GetType().GetElementType();
                CustomAttributeTypedArgument[] new_value = new CustomAttributeTypedArgument [a.GetLength(0)];
                for (int i = 0; i < new_value.Length; ++i)
                {
                    new_value [i] = new CustomAttributeTypedArgument(etype, a.GetValue(i));
                }
                this.value = new ReadOnlyCollection <CustomAttributeTypedArgument> (new_value);
            }
        }
Ejemplo n.º 29
0
        internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncodedArgument encodedArg)
        {
            CustomAttributeEncoding encodedType = encodedArg.CustomAttributeType.EncodedType;

            if (encodedType == CustomAttributeEncoding.Undefined)
                throw new ArgumentException("encodedArg");

            else if (encodedType == CustomAttributeEncoding.Enum)
            {
                m_argumentType = ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
                m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedArg.CustomAttributeType.EncodedEnumType);
            }
            else if (encodedType == CustomAttributeEncoding.String)
            {
                m_argumentType = typeof(string);
                m_value = encodedArg.StringValue;
            }
            else if (encodedType == CustomAttributeEncoding.Type)
            {
                m_argumentType = typeof(Type);
                
                m_value = null;

                if (encodedArg.StringValue != null)
                    m_value = ResolveType(scope, encodedArg.StringValue);
            }
            else if (encodedType == CustomAttributeEncoding.Array)
            {                
                encodedType = encodedArg.CustomAttributeType.EncodedArrayType;
                Type elementType;
                
                if (encodedType == CustomAttributeEncoding.Enum)
                {
                    elementType = ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
                }
                else
                {
                    elementType = CustomAttributeEncodingToType(encodedType);
                }

                m_argumentType = elementType.MakeArrayType();
        
                if (encodedArg.ArrayValue == null)
                {
                    m_value = null;                    
                }
                else
                {
                    CustomAttributeTypedArgument[] arrayValue = new CustomAttributeTypedArgument[encodedArg.ArrayValue.Length];
                    for (int i = 0; i < arrayValue.Length; i++)
                        arrayValue[i] = new CustomAttributeTypedArgument(scope, encodedArg.ArrayValue[i]);

                    m_value = Array.AsReadOnly(arrayValue);
                }
            }
            else
            {
                m_argumentType = CustomAttributeEncodingToType(encodedType);
                m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedType);
            }
        }
Ejemplo n.º 30
0
        public CustomAttributeNamedArgument(MemberInfo memberInfo, object value)
        {
            if (memberInfo == null)
                throw new ArgumentNullException("memberInfo");

            Type type = null;
            FieldInfo field = memberInfo as FieldInfo;
            PropertyInfo property = memberInfo as PropertyInfo;

            if (field != null)
                type = field.FieldType;
            else if (property != null)
                type = property.PropertyType;
            else
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidMemberForNamedArgument"));

            m_memberInfo = memberInfo;
            m_value = new CustomAttributeTypedArgument(type, value);
        }
Ejemplo n.º 31
0
        public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
        {
            if (memberInfo == null)
                throw new ArgumentNullException("memberInfo");

            m_memberInfo = memberInfo;
            m_value = typedArgument;
        }
        internal CustomAttributeTypedArgument(Module scope, CustomAttributeEncodedArgument encodedArg)
        {
            CustomAttributeEncoding encodedType = encodedArg.CustomAttributeType.EncodedType;
            switch (encodedType)
            {
                case CustomAttributeEncoding.Undefined:
                    throw new ArgumentException("encodedArg");

                case CustomAttributeEncoding.Enum:
                    this.m_argumentType = ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
                    this.m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedArg.CustomAttributeType.EncodedEnumType);
                    return;

                case CustomAttributeEncoding.String:
                    this.m_argumentType = typeof(string);
                    this.m_value = encodedArg.StringValue;
                    return;

                case CustomAttributeEncoding.Type:
                    this.m_argumentType = typeof(Type);
                    this.m_value = null;
                    if (encodedArg.StringValue != null)
                    {
                        this.m_value = ResolveType(scope, encodedArg.StringValue);
                        return;
                    }
                    break;

                case CustomAttributeEncoding.Array:
                {
                    Type type;
                    encodedType = encodedArg.CustomAttributeType.EncodedArrayType;
                    if (encodedType == CustomAttributeEncoding.Enum)
                    {
                        type = ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
                    }
                    else
                    {
                        type = CustomAttributeEncodingToType(encodedType);
                    }
                    this.m_argumentType = type.MakeArrayType();
                    if (encodedArg.ArrayValue == null)
                    {
                        this.m_value = null;
                        return;
                    }
                    CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[encodedArg.ArrayValue.Length];
                    for (int i = 0; i < array.Length; i++)
                    {
                        array[i] = new CustomAttributeTypedArgument(scope, encodedArg.ArrayValue[i]);
                    }
                    this.m_value = Array.AsReadOnly<CustomAttributeTypedArgument>(array);
                    return;
                }
                default:
                    this.m_argumentType = CustomAttributeEncodingToType(encodedType);
                    this.m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedType);
                    break;
            }
        }
Ejemplo n.º 33
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);
        }
Ejemplo n.º 34
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        internal Object GetDefaultValue(bool raw) 
        {
            // Cannot cache because default value could be non-agile user defined enumeration.
            object defaultValue = null;
 
            // for dynamic method we pretend to have cached the value so we do not go to metadata
            if (!m_noDefaultValue) 
            { 
                if (ParameterType == typeof(DateTime))
                { 
                    if (raw)
                    {
                        CustomAttributeTypedArgument value =
                            CustomAttributeData.Filter( 
                                CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0);
 
                        if (value.ArgumentType != null) 
                            return new DateTime((long)value.Value);
                    } 
                    else
                    {
                        object[] dt = GetCustomAttributes(typeof(DateTimeConstantAttribute), false);
                        if (dt != null && dt.Length != 0) 
                            return ((DateTimeConstantAttribute)dt[0]).Value;
                    } 
                } 

                #region Look for a default value in metadata 
                if (!MdToken.IsNullToken(m_tkParamDef))
                {
                    defaultValue = MdConstant.GetValue(m_scope, m_tkParamDef, ParameterType.GetTypeHandleInternal(), raw);
                } 
                #endregion
 
                if (defaultValue == DBNull.Value) 
                {
                    #region Look for a default value in the custom attributes 
                    if (raw)
                    {
                        System.Collections.Generic.IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes(this);
                        CustomAttributeTypedArgument value = CustomAttributeData.Filter( 
                            attrs, s_CustomConstantAttributeType, "Value");
 
                        if (value.ArgumentType == null) 
                        {
                            value = CustomAttributeData.Filter( 
                                attrs, s_DecimalConstantAttributeType, "Value");


                            if (value.ArgumentType == null) 
                            {
                                for (int i = 0; i < attrs.Count; i++) 
                                { 
                                    if (attrs[i].Constructor.DeclaringType == s_DecimalConstantAttributeType)
                                    { 
                                        ParameterInfo[] parameters = attrs[i].Constructor.GetParameters();

                                        if (parameters.Length != 0)
                                        { 
                                            if (parameters[2].ParameterType == typeof(uint))
                                            { 
                                                System.Collections.Generic.IList<CustomAttributeTypedArgument> args = attrs[i].ConstructorArguments; 
                                                int low = (int)(UInt32)args[4].Value;
                                                int mid = (int)(UInt32)args[3].Value; 
                                                int hi = (int)(UInt32)args[2].Value;
                                                byte sign = (byte)args[1].Value;
                                                byte scale = (byte)args[0].Value;
                                                value = new CustomAttributeTypedArgument( 
                                                    new System.Decimal(low, mid, hi, (sign != 0), scale));
                                            } 
                                            else 
                                            {
                                                System.Collections.Generic.IList<CustomAttributeTypedArgument> args = attrs[i].ConstructorArguments; 
                                                int low = (int)args[4].Value;
                                                int mid = (int)args[3].Value;
                                                int hi = (int)args[2].Value;
                                                byte sign = (byte)args[1].Value; 
                                                byte scale = (byte)args[0].Value;
                                                value = new CustomAttributeTypedArgument( 
                                                    new System.Decimal(low, mid, hi, (sign != 0), scale)); 
                                            }
                                        } 
                                    }
                                }
                            }
                        } 

                        if (value.ArgumentType != null) 
                            defaultValue = value.Value; 
                    }
                    else 
                    {
                        Object[] CustomAttrs = GetCustomAttributes(s_CustomConstantAttributeType, false);
                        if (CustomAttrs.Length != 0)
                        { 
                            defaultValue = ((CustomConstantAttribute)CustomAttrs[0]).Value;
                        } 
                        else 
                        {
                            CustomAttrs = GetCustomAttributes(s_DecimalConstantAttributeType, false); 
                            if (CustomAttrs.Length != 0)
                            {
                                defaultValue = ((DecimalConstantAttribute)CustomAttrs[0]).Value;
                            } 
                        }
                    } 
                    #endregion 
                }
 
                if (defaultValue == DBNull.Value)
                {
                    #region Handle case if no default value was found
                    if (IsOptional) 
                    {
                        // If the argument is marked as optional then the default value is Missing.Value. 
                        defaultValue = Type.Missing; 
                    }
                    #endregion 
                }

            }
 
            return defaultValue;
        } 
Ejemplo n.º 35
0
 public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
 {
 }
		internal CustomAttributeNamedArgument (MemberInfo memberInfo, object typedArgument)
		{
			this.memberInfo = memberInfo;
			this.typedArgument = (CustomAttributeTypedArgument) typedArgument;
		}
        private object GetDefaultValueInternal(bool raw)
        {
            if (this.m_noDefaultValue)
            {
                return(DBNull.Value);
            }
            object obj = null;

            if (this.ParameterType == typeof(DateTime))
            {
                if (raw)
                {
                    CustomAttributeTypedArgument customAttributeTypedArgument = CustomAttributeData.Filter(CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0);
                    if (customAttributeTypedArgument.ArgumentType != null)
                    {
                        return(new DateTime((long)customAttributeTypedArgument.Value));
                    }
                }
                else
                {
                    object[] customAttributes = this.GetCustomAttributes(typeof(DateTimeConstantAttribute), false);
                    if (customAttributes != null && customAttributes.Length != 0)
                    {
                        return(((DateTimeConstantAttribute)customAttributes[0]).Value);
                    }
                }
            }
            if (!System.Reflection.MetadataToken.IsNullToken(this.m_tkParamDef))
            {
                obj = MdConstant.GetValue(this.m_scope, this.m_tkParamDef, this.ParameterType.GetTypeHandleInternal(), raw);
            }
            if (obj == DBNull.Value)
            {
                if (raw)
                {
                    using (IEnumerator <CustomAttributeData> enumerator = CustomAttributeData.GetCustomAttributes(this).GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            CustomAttributeData customAttributeData = enumerator.Current;
                            Type declaringType = customAttributeData.Constructor.DeclaringType;
                            if (declaringType == typeof(DateTimeConstantAttribute))
                            {
                                obj = DateTimeConstantAttribute.GetRawDateTimeConstant(customAttributeData);
                            }
                            else if (declaringType == typeof(DecimalConstantAttribute))
                            {
                                obj = DecimalConstantAttribute.GetRawDecimalConstant(customAttributeData);
                            }
                            else if (declaringType.IsSubclassOf(RuntimeParameterInfo.s_CustomConstantAttributeType))
                            {
                                obj = CustomConstantAttribute.GetRawConstant(customAttributeData);
                            }
                        }
                        goto IL_1A7;
                    }
                }
                object[] customAttributes2 = this.GetCustomAttributes(RuntimeParameterInfo.s_CustomConstantAttributeType, false);
                if (customAttributes2.Length != 0)
                {
                    obj = ((CustomConstantAttribute)customAttributes2[0]).Value;
                }
                else
                {
                    customAttributes2 = this.GetCustomAttributes(RuntimeParameterInfo.s_DecimalConstantAttributeType, false);
                    if (customAttributes2.Length != 0)
                    {
                        obj = ((DecimalConstantAttribute)customAttributes2[0]).Value;
                    }
                }
            }
IL_1A7:
            if (obj == DBNull.Value)
            {
                this.m_noDefaultValue = true;
            }
            return(obj);
        }
 // These APIs are to detect and support ReflectionOnlyLoadFrom which is used at compile time. 
 // If it isn't the reflection only case, then the normal API to get attributes is used.
 //
 // At compile time, the only custom attributes which we extract using GetCustomAttributes are:
 //  Microsoft.JScript.JSFunctionAttribute
 //  Microsoft.JScript.NotRecommended
 //  Microsoft.JScript.ReferenceAttribute
 //  System.AttributeUsageAttribute
 //  System.CLSCompliantAttribute
 //  System.ObsoleteAttribute
 //  System.Runtime.InteropServices.CoClassAttribute
 //  System.Security.AllowPartiallyTrustedCallersAttribute
 //
 // The only ones we check for using IsDefined are:
 //  Microsoft.JScript.Expando
 //  Microsoft.JScript.JSFunctionAttribute
 //  System.ParamArrayAttribute
 //  System.Runtime.CompilerServices.RequiredAttributeAttribute
 //    
 // None of these are Inherited attibutes and so the ReflectionOnly code path can ignore the 
 // inherit flag. The System.* attributes are sealed. The Microsoft.JScript ones are not, 
 // though they should be and the compiler will not respect subtypes of these attributes.
 //
 private static Object GetCustomAttributeValue(CustomAttributeTypedArgument arg) {
   Type reflectionOnlyArgType = arg.ArgumentType;
   
   // If it's an enumerated type, the value is the boxed underlying value and must be converted.
   if (reflectionOnlyArgType.IsEnum)
     return Enum.ToObject(Type.GetType(reflectionOnlyArgType.FullName), arg.Value);
   
   return arg.Value;
 }
Ejemplo n.º 39
0
 private void Init(FieldOffsetAttribute fieldOffset)
 {
     this.m_ctor = typeof(FieldOffsetAttribute).GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
     CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(fieldOffset.Value) };
     this.m_typedCtorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>(array);
     this.m_namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>(new CustomAttributeNamedArgument[0]);
 }
Ejemplo n.º 40
0
 public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
 {
     _memberInfo = memberInfo ?? throw new ArgumentNullException(nameof(memberInfo));
     _value      = typedArgument;
 }
 internal CustomAttributeNamedArgument(System.Reflection.MemberInfo memberInfo, CustomAttributeTypedArgument value)
 {
     this.m_memberInfo = memberInfo;
     this.m_value      = value;
 }
		CustomAttributeNamedArgument (MemberInfo memberInfo, object value)
		{
			this.memberInfo = memberInfo;
			this.typedArgument = (CustomAttributeTypedArgument) value;
		}
Ejemplo n.º 43
0
 internal CustomAttributeNamedArgument(Type attributeType, string memberName, bool isField, CustomAttributeTypedArgument typedValue) { }
Ejemplo n.º 44
0
        /// <summary>
        /// Returns a valid C# representation of a typed argument that can be 
        /// used in a custom attribute for code generation.
        /// </summary>
        public string ToCSharpString(CustomAttributeTypedArgument argument)
        {
            using (var provider = new Microsoft.CSharp.CSharpCodeProvider())
            using (var writer = new System.IO.StringWriter(CultureInfo.InvariantCulture))
            {
                if (argument.Value == null)
                {
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(null), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                    return writer.ToString();
                }

                if (argument.ArgumentType == typeof(string))
                    return "\"" + argument.Value + "\"";

                if (argument.ArgumentType.IsEnum)
                    return argument.ArgumentType.Name + "." + Enum.GetName(argument.ArgumentType, argument.Value);

                if (argument.ArgumentType == typeof(Type))
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodeTypeOfExpression(((Type)argument.Value).Name), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                else
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(argument.Value), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());

                return writer.ToString();
            }
        }
 private void Init(TypeForwardedToAttribute forwardedTo)
 {
     Type type = typeof(TypeForwardedToAttribute);
     Type[] types = new Type[] { typeof(Type) };
     this.m_ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, types, null);
     CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(Type), forwardedTo.Destination) };
     this.m_typedCtorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>(array);
     CustomAttributeNamedArgument[] argumentArray2 = new CustomAttributeNamedArgument[0];
     this.m_namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>(argumentArray2);
 }
Ejemplo n.º 46
0
 public static CustomAttributeNamedArgument CreateCustomAttributeNamedArgument(Type attributeType, string memberName, bool isField, CustomAttributeTypedArgument typedValue)
 {
     return new CustomAttributeNamedArgument(attributeType, memberName, isField, typedValue);
 }
Ejemplo n.º 47
0
        private CustomAttributeData?GetDllImportAttributeData()
        {
            if ((Attributes & MethodAttributes.PinvokeImpl) == 0)
            {
                return(null);
            }

            string            entryPoint;
            string?           dllName = null;
            PInvokeAttributes flags   = 0;

            GetPInvoke(out flags, out entryPoint, out dllName);

            CharSet charSet = (flags & PInvokeAttributes.CharSetMask) switch
            {
                PInvokeAttributes.CharSetNotSpec => CharSet.None,
                PInvokeAttributes.CharSetAnsi => CharSet.Ansi,
                PInvokeAttributes.CharSetUnicode => CharSet.Unicode,
                PInvokeAttributes.CharSetAuto => CharSet.Auto,
                // Invalid: default to CharSet.None
                _ => CharSet.None,
            };

            InteropServicesCallingConvention callingConvention = (flags & PInvokeAttributes.CallConvMask) switch
            {
                PInvokeAttributes.CallConvWinapi => InteropServicesCallingConvention.Winapi,
                PInvokeAttributes.CallConvCdecl => InteropServicesCallingConvention.Cdecl,
                PInvokeAttributes.CallConvStdcall => InteropServicesCallingConvention.StdCall,
                PInvokeAttributes.CallConvThiscall => InteropServicesCallingConvention.ThisCall,
                PInvokeAttributes.CallConvFastcall => InteropServicesCallingConvention.FastCall,
                // Invalid: default to CallingConvention.Cdecl
                _ => InteropServicesCallingConvention.Cdecl,
            };

            bool exactSpelling         = (flags & PInvokeAttributes.NoMangle) != 0;
            bool setLastError          = (flags & PInvokeAttributes.SupportsLastError) != 0;
            bool bestFitMapping        = (flags & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (flags & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
            bool preserveSig           = (GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;

            var ctorArgs = new CustomAttributeTypedArgument[] {
                new CustomAttributeTypedArgument(typeof(string), dllName),
            };

            Type attrType = typeof(DllImportAttribute);

            var namedArgs = new CustomAttributeNamedArgument[] {
                new CustomAttributeNamedArgument(attrType.GetField("EntryPoint") !, entryPoint),
                new CustomAttributeNamedArgument(attrType.GetField("CharSet") !, charSet),
                new CustomAttributeNamedArgument(attrType.GetField("ExactSpelling") !, exactSpelling),
                new CustomAttributeNamedArgument(attrType.GetField("SetLastError") !, setLastError),
                new CustomAttributeNamedArgument(attrType.GetField("PreserveSig") !, preserveSig),
                new CustomAttributeNamedArgument(attrType.GetField("CallingConvention") !, callingConvention),
                new CustomAttributeNamedArgument(attrType.GetField("BestFitMapping") !, bestFitMapping),
                new CustomAttributeNamedArgument(attrType.GetField("ThrowOnUnmappableChar") !, throwOnUnmappableChar)
            };

            return(new CustomAttributeData(
                       attrType.GetConstructor(new[] { typeof(string) }) !,
                       ctorArgs,
                       namedArgs));
        }
        public CustomAttributeTypedArgument CreateTypedArgumentStruct(Type type, object value)
        {
            // Because C# doesn't like structs which contain object references to be referenced by pointers
            // we need to use a special MOSA compiler trick to get its address to create a pointer
            CustomAttributeTypedArgument typedArgument = new CustomAttributeTypedArgument();
            var ptr = (uint**)Intrinsic.GetValueTypeAddress(typedArgument);
            ptr[0] = (uint*)Intrinsic.GetObjectAddress(type);
            ptr[1] = (uint*)Intrinsic.GetObjectAddress(value);

            return typedArgument;
        }
Ejemplo n.º 49
0
        private Object GetDefaultValueInternal(bool raw)
        {
            Contract.Assert(!m_noMetadata);

            if (m_noDefaultValue)
            {
                return(DBNull.Value);
            }

            object defaultValue = null;

            // Why check the parameter type only for DateTime and only for the ctor arguments?
            // No check on the parameter type is done for named args and for Decimal.

            // We should move this after MdToken.IsNullToken(m_tkParamDef) and combine it
            // with the other custom attribute logic. But will that be a breaking change?
            // For a DateTime parameter on which both an md constant and a ca constant are set,
            // which one should win?
            if (ParameterType == typeof(DateTime))
            {
                if (raw)
                {
                    CustomAttributeTypedArgument value =
                        CustomAttributeData.Filter(
                            CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0);

                    if (value.ArgumentType != null)
                    {
                        return(new DateTime((long)value.Value));
                    }
                }
                else
                {
                    object[] dt = GetCustomAttributes(typeof(DateTimeConstantAttribute), false);
                    if (dt != null && dt.Length != 0)
                    {
                        return(((DateTimeConstantAttribute)dt[0]).Value);
                    }
                }
            }

            #region Look for a default value in metadata
            if (!MdToken.IsNullToken(m_tkParamDef))
            {
                // This will return DBNull.Value if no constant value is defined on m_tkParamDef in the metadata.
                defaultValue = MdConstant.GetValue(m_scope, m_tkParamDef, ParameterType.GetTypeHandleInternal(), raw);
            }
            #endregion

            if (defaultValue == DBNull.Value)
            {
                #region Look for a default value in the custom attributes
                if (raw)
                {
                    foreach (CustomAttributeData attr in CustomAttributeData.GetCustomAttributes(this))
                    {
                        Type attrType = attr.Constructor.DeclaringType;

                        if (attrType == typeof(DateTimeConstantAttribute))
                        {
                            defaultValue = DateTimeConstantAttribute.GetRawDateTimeConstant(attr);
                        }
                        else if (attrType == typeof(DecimalConstantAttribute))
                        {
                            defaultValue = DecimalConstantAttribute.GetRawDecimalConstant(attr);
                        }
                        else if (attrType.IsSubclassOf(s_CustomConstantAttributeType))
                        {
                            defaultValue = CustomConstantAttribute.GetRawConstant(attr);
                        }
                    }
                }
                else
                {
                    Object[] CustomAttrs = GetCustomAttributes(s_CustomConstantAttributeType, false);
                    if (CustomAttrs.Length != 0)
                    {
                        defaultValue = ((CustomConstantAttribute)CustomAttrs[0]).Value;
                    }
                    else
                    {
                        CustomAttrs = GetCustomAttributes(s_DecimalConstantAttributeType, false);
                        if (CustomAttrs.Length != 0)
                        {
                            defaultValue = ((DecimalConstantAttribute)CustomAttrs[0]).Value;
                        }
                    }
                }
                #endregion
            }

            if (defaultValue == DBNull.Value)
            {
                m_noDefaultValue = true;
            }

            return(defaultValue);
        }
Ejemplo n.º 50
0
        private void Init(TypeForwardedToAttribute forwardedTo)
        {
            Type type = typeof(TypeForwardedToAttribute);

            Type[] sig = new Type[] { typeof(Type) };
            m_ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, sig, null);

            CustomAttributeTypedArgument[] typedArgs = new CustomAttributeTypedArgument[1];
            typedArgs[0] = new CustomAttributeTypedArgument(typeof(Type), forwardedTo.Destination);
            m_typedCtorArgs = Array.AsReadOnly(typedArgs);

            CustomAttributeNamedArgument[] namedArgs = new CustomAttributeNamedArgument[0];
            m_namedArgs = Array.AsReadOnly(namedArgs);
        }
Ejemplo n.º 51
0
        internal object GetDefaultValue(bool raw)
        {
            object missing = null;

            if (!this.m_noDefaultValue)
            {
                if (this.ParameterType == typeof(DateTime))
                {
                    if (raw)
                    {
                        CustomAttributeTypedArgument argument = CustomAttributeData.Filter(CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0);
                        if (argument.ArgumentType != null)
                        {
                            return(new DateTime((long)argument.Value));
                        }
                    }
                    else
                    {
                        object[] customAttributes = this.GetCustomAttributes(typeof(DateTimeConstantAttribute), false);
                        if ((customAttributes != null) && (customAttributes.Length != 0))
                        {
                            return(((DateTimeConstantAttribute)customAttributes[0]).Value);
                        }
                    }
                }
                if (!System.Reflection.MetadataToken.IsNullToken(this.m_tkParamDef))
                {
                    missing = MdConstant.GetValue(this.m_scope, this.m_tkParamDef, this.ParameterType.GetTypeHandleInternal(), raw);
                }
                if (missing == DBNull.Value)
                {
                    if (raw)
                    {
                        IList <CustomAttributeData>  attrs     = CustomAttributeData.GetCustomAttributes(this);
                        CustomAttributeTypedArgument argument2 = CustomAttributeData.Filter(attrs, s_CustomConstantAttributeType, "Value");
                        if (argument2.ArgumentType == null)
                        {
                            argument2 = CustomAttributeData.Filter(attrs, s_DecimalConstantAttributeType, "Value");
                            if (argument2.ArgumentType == null)
                            {
                                for (int i = 0; i < attrs.Count; i++)
                                {
                                    if (attrs[i].Constructor.DeclaringType == s_DecimalConstantAttributeType)
                                    {
                                        ParameterInfo[] parameters = attrs[i].Constructor.GetParameters();
                                        if (parameters.Length != 0)
                                        {
                                            if (parameters[2].ParameterType == typeof(uint))
                                            {
                                                IList <CustomAttributeTypedArgument> constructorArguments = attrs[i].ConstructorArguments;
                                                CustomAttributeTypedArgument         argument3            = constructorArguments[4];
                                                int lo = (int)((uint)argument3.Value);
                                                CustomAttributeTypedArgument argument4 = constructorArguments[3];
                                                int mid = (int)((uint)argument4.Value);
                                                CustomAttributeTypedArgument argument5 = constructorArguments[2];
                                                int hi = (int)((uint)argument5.Value);
                                                CustomAttributeTypedArgument argument6 = constructorArguments[1];
                                                byte num5 = (byte)argument6.Value;
                                                CustomAttributeTypedArgument argument7 = constructorArguments[0];
                                                byte scale = (byte)argument7.Value;
                                                argument2 = new CustomAttributeTypedArgument(new decimal(lo, mid, hi, num5 != 0, scale));
                                            }
                                            else
                                            {
                                                IList <CustomAttributeTypedArgument> list3     = attrs[i].ConstructorArguments;
                                                CustomAttributeTypedArgument         argument8 = list3[4];
                                                int num7 = (int)argument8.Value;
                                                CustomAttributeTypedArgument argument9 = list3[3];
                                                int num8 = (int)argument9.Value;
                                                CustomAttributeTypedArgument argument10 = list3[2];
                                                int num9 = (int)argument10.Value;
                                                CustomAttributeTypedArgument argument11 = list3[1];
                                                byte num10 = (byte)argument11.Value;
                                                CustomAttributeTypedArgument argument12 = list3[0];
                                                byte num11 = (byte)argument12.Value;
                                                argument2 = new CustomAttributeTypedArgument(new decimal(num7, num8, num9, num10 != 0, num11));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (argument2.ArgumentType != null)
                        {
                            missing = argument2.Value;
                        }
                    }
                    else
                    {
                        object[] objArray2 = this.GetCustomAttributes(s_CustomConstantAttributeType, false);
                        if (objArray2.Length != 0)
                        {
                            missing = ((CustomConstantAttribute)objArray2[0]).Value;
                        }
                        else
                        {
                            objArray2 = this.GetCustomAttributes(s_DecimalConstantAttributeType, false);
                            if (objArray2.Length != 0)
                            {
                                missing = ((DecimalConstantAttribute)objArray2[0]).Value;
                            }
                        }
                    }
                }
                if ((missing == DBNull.Value) && this.IsOptional)
                {
                    missing = Type.Missing;
                }
            }
            return(missing);
        }
		public CustomAttributeNamedArgument (MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
		{
			this.memberInfo = memberInfo;
			this.typedArgument = typedArgument;
		}
 // This constructor is the one used by .Net Native as the current metadata format only contains the name and the "isField" value,
 // not the actual member. To keep .Net Native running as before, we'll use the name and isField as the principal data and
 // construct the MemberInfo on demand.
 internal CustomAttributeNamedArgument(Type attributeType, string memberName, bool isField, CustomAttributeTypedArgument typedValue)
 {
     IsField         = isField;
     MemberName      = memberName;
     TypedValue      = typedValue;
     _attributeType  = attributeType;
     _lazyMemberInfo = null;
 }
Ejemplo n.º 54
0
 internal object GetDefaultValue(bool raw)
 {
     object missing = null;
     if (!this.m_noDefaultValue)
     {
         if (this.ParameterType == typeof(DateTime))
         {
             if (raw)
             {
                 CustomAttributeTypedArgument argument = CustomAttributeData.Filter(CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0);
                 if (argument.ArgumentType != null)
                 {
                     return new DateTime((long) argument.Value);
                 }
             }
             else
             {
                 object[] customAttributes = this.GetCustomAttributes(typeof(DateTimeConstantAttribute), false);
                 if ((customAttributes != null) && (customAttributes.Length != 0))
                 {
                     return ((DateTimeConstantAttribute) customAttributes[0]).Value;
                 }
             }
         }
         if (!System.Reflection.MetadataToken.IsNullToken(this.m_tkParamDef))
         {
             missing = MdConstant.GetValue(this.m_scope, this.m_tkParamDef, this.ParameterType.GetTypeHandleInternal(), raw);
         }
         if (missing == DBNull.Value)
         {
             if (raw)
             {
                 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes(this);
                 CustomAttributeTypedArgument argument2 = CustomAttributeData.Filter(attrs, s_CustomConstantAttributeType, "Value");
                 if (argument2.ArgumentType == null)
                 {
                     argument2 = CustomAttributeData.Filter(attrs, s_DecimalConstantAttributeType, "Value");
                     if (argument2.ArgumentType == null)
                     {
                         for (int i = 0; i < attrs.Count; i++)
                         {
                             if (attrs[i].Constructor.DeclaringType == s_DecimalConstantAttributeType)
                             {
                                 ParameterInfo[] parameters = attrs[i].Constructor.GetParameters();
                                 if (parameters.Length != 0)
                                 {
                                     if (parameters[2].ParameterType == typeof(uint))
                                     {
                                         IList<CustomAttributeTypedArgument> constructorArguments = attrs[i].ConstructorArguments;
                                         CustomAttributeTypedArgument argument3 = constructorArguments[4];
                                         int lo = (int) ((uint) argument3.Value);
                                         CustomAttributeTypedArgument argument4 = constructorArguments[3];
                                         int mid = (int) ((uint) argument4.Value);
                                         CustomAttributeTypedArgument argument5 = constructorArguments[2];
                                         int hi = (int) ((uint) argument5.Value);
                                         CustomAttributeTypedArgument argument6 = constructorArguments[1];
                                         byte num5 = (byte) argument6.Value;
                                         CustomAttributeTypedArgument argument7 = constructorArguments[0];
                                         byte scale = (byte) argument7.Value;
                                         argument2 = new CustomAttributeTypedArgument(new decimal(lo, mid, hi, num5 != 0, scale));
                                     }
                                     else
                                     {
                                         IList<CustomAttributeTypedArgument> list3 = attrs[i].ConstructorArguments;
                                         CustomAttributeTypedArgument argument8 = list3[4];
                                         int num7 = (int) argument8.Value;
                                         CustomAttributeTypedArgument argument9 = list3[3];
                                         int num8 = (int) argument9.Value;
                                         CustomAttributeTypedArgument argument10 = list3[2];
                                         int num9 = (int) argument10.Value;
                                         CustomAttributeTypedArgument argument11 = list3[1];
                                         byte num10 = (byte) argument11.Value;
                                         CustomAttributeTypedArgument argument12 = list3[0];
                                         byte num11 = (byte) argument12.Value;
                                         argument2 = new CustomAttributeTypedArgument(new decimal(num7, num8, num9, num10 != 0, num11));
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if (argument2.ArgumentType != null)
                 {
                     missing = argument2.Value;
                 }
             }
             else
             {
                 object[] objArray2 = this.GetCustomAttributes(s_CustomConstantAttributeType, false);
                 if (objArray2.Length != 0)
                 {
                     missing = ((CustomConstantAttribute) objArray2[0]).Value;
                 }
                 else
                 {
                     objArray2 = this.GetCustomAttributes(s_DecimalConstantAttributeType, false);
                     if (objArray2.Length != 0)
                     {
                         missing = ((DecimalConstantAttribute) objArray2[0]).Value;
                     }
                 }
             }
         }
         if ((missing == DBNull.Value) && this.IsOptional)
         {
             missing = Type.Missing;
         }
     }
     return missing;
 }
Ejemplo n.º 55
0
 private void Init(DllImportAttribute dllImport)
 {
     Type type = typeof(DllImportAttribute);
     this.m_ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
     CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(dllImport.Value) };
     this.m_typedCtorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>(array);
     CustomAttributeNamedArgument[] argumentArray2 = new CustomAttributeNamedArgument[] { new CustomAttributeNamedArgument(type.GetField("EntryPoint"), dllImport.EntryPoint), new CustomAttributeNamedArgument(type.GetField("CharSet"), dllImport.CharSet), new CustomAttributeNamedArgument(type.GetField("ExactSpelling"), dllImport.ExactSpelling), new CustomAttributeNamedArgument(type.GetField("SetLastError"), dllImport.SetLastError), new CustomAttributeNamedArgument(type.GetField("PreserveSig"), dllImport.PreserveSig), new CustomAttributeNamedArgument(type.GetField("CallingConvention"), dllImport.CallingConvention), new CustomAttributeNamedArgument(type.GetField("BestFitMapping"), dllImport.BestFitMapping), new CustomAttributeNamedArgument(type.GetField("ThrowOnUnmappableChar"), dllImport.ThrowOnUnmappableChar) };
     this.m_namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>(argumentArray2);
 }
        // Token: 0x0600443D RID: 17469 RVA: 0x000FA6D0 File Offset: 0x000F88D0
        internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncodedArgument encodedArg)
        {
            CustomAttributeEncoding customAttributeEncoding = encodedArg.CustomAttributeType.EncodedType;

            if (customAttributeEncoding == CustomAttributeEncoding.Undefined)
            {
                throw new ArgumentException("encodedArg");
            }
            if (customAttributeEncoding == CustomAttributeEncoding.Enum)
            {
                this.m_argumentType = CustomAttributeTypedArgument.ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
                this.m_value        = CustomAttributeTypedArgument.EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedArg.CustomAttributeType.EncodedEnumType);
                return;
            }
            if (customAttributeEncoding == CustomAttributeEncoding.String)
            {
                this.m_argumentType = typeof(string);
                this.m_value        = encodedArg.StringValue;
                return;
            }
            if (customAttributeEncoding == CustomAttributeEncoding.Type)
            {
                this.m_argumentType = typeof(Type);
                this.m_value        = null;
                if (encodedArg.StringValue != null)
                {
                    this.m_value = CustomAttributeTypedArgument.ResolveType(scope, encodedArg.StringValue);
                    return;
                }
            }
            else if (customAttributeEncoding == CustomAttributeEncoding.Array)
            {
                customAttributeEncoding = encodedArg.CustomAttributeType.EncodedArrayType;
                Type type;
                if (customAttributeEncoding == CustomAttributeEncoding.Enum)
                {
                    type = CustomAttributeTypedArgument.ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
                }
                else
                {
                    type = CustomAttributeTypedArgument.CustomAttributeEncodingToType(customAttributeEncoding);
                }
                this.m_argumentType = type.MakeArrayType();
                if (encodedArg.ArrayValue == null)
                {
                    this.m_value = null;
                    return;
                }
                CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[encodedArg.ArrayValue.Length];
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = new CustomAttributeTypedArgument(scope, encodedArg.ArrayValue[i]);
                }
                this.m_value = Array.AsReadOnly <CustomAttributeTypedArgument>(array);
                return;
            }
            else
            {
                this.m_argumentType = CustomAttributeTypedArgument.CustomAttributeEncodingToType(customAttributeEncoding);
                this.m_value        = CustomAttributeTypedArgument.EncodedValueToRawValue(encodedArg.PrimitiveValue, customAttributeEncoding);
            }
        }
Ejemplo n.º 57
0
 private void Init(MarshalAsAttribute marshalAs)
 {
     Type type = typeof(MarshalAsAttribute);
     this.m_ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
     CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(marshalAs.Value) };
     this.m_typedCtorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>(array);
     int num = 3;
     if (marshalAs.MarshalType != null)
     {
         num++;
     }
     if (marshalAs.MarshalTypeRef != null)
     {
         num++;
     }
     if (marshalAs.MarshalCookie != null)
     {
         num++;
     }
     num++;
     num++;
     if (marshalAs.SafeArrayUserDefinedSubType != null)
     {
         num++;
     }
     CustomAttributeNamedArgument[] argumentArray = new CustomAttributeNamedArgument[num];
     num = 0;
     argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("ArraySubType"), marshalAs.ArraySubType);
     argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SizeParamIndex"), marshalAs.SizeParamIndex);
     argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SizeConst"), marshalAs.SizeConst);
     argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("IidParameterIndex"), marshalAs.IidParameterIndex);
     argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SafeArraySubType"), marshalAs.SafeArraySubType);
     if (marshalAs.MarshalType != null)
     {
         argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("MarshalType"), marshalAs.MarshalType);
     }
     if (marshalAs.MarshalTypeRef != null)
     {
         argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("MarshalTypeRef"), marshalAs.MarshalTypeRef);
     }
     if (marshalAs.MarshalCookie != null)
     {
         argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("MarshalCookie"), marshalAs.MarshalCookie);
     }
     if (marshalAs.SafeArrayUserDefinedSubType != null)
     {
         argumentArray[num++] = new CustomAttributeNamedArgument(type.GetField("SafeArrayUserDefinedSubType"), marshalAs.SafeArrayUserDefinedSubType);
     }
     this.m_namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>(argumentArray);
 }
Ejemplo n.º 58
0
 internal CustomAttributeNamedArgument(MemberInfo memberInfo, object typedArgument)
 {
     this.memberInfo    = memberInfo;
     this.typedArgument = (CustomAttributeTypedArgument)typedArgument;
 }
 /// <summary>Initializes a new instance of the <see cref="T:System.Reflection.CustomAttributeNamedArgument" /> class, which represents the specified field or property of the custom attribute, and specifies a <see cref="T:System.Reflection.CustomAttributeTypedArgument" /> object that describes the type and value of the field or property.</summary><param name="memberInfo">A field or property of the custom attribute. The new <see cref="T:System.Reflection.CustomAttributeNamedArgument" /> object represents this member and its value.</param><param name="typedArgument">An object that describes the type and value of the field or property.</param><exception cref="T:System.ArgumentNullException"><paramref name="memberInfo" /> is null.</exception>
 public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 60
0
        private static IEnumerable<XObject> GenerateAttributeArgument(IProcessingContext context,
                                                                      CustomAttributeTypedArgument cata)
        {
            context.AddReference(AssetIdentifier.FromMemberInfo(cata.ArgumentType));
            yield return new XAttribute("type", AssetIdentifier.FromMemberInfo(cata.ArgumentType));

            if (cata.ArgumentType.IsEnum)
            {
                if (
                    cata.ArgumentType.GetCustomAttributesData().Any(
                                                                    ca =>
                                                                    ca.Constructor.DeclaringType ==
                                                                    typeof(FlagsAttribute)))
                {
                    var parts = GetEnumFlags(cata.ArgumentType, cata.Value).ToArray();

                    yield return
                        new XElement("enum",
                                     Array.ConvertAll(parts,
                                                      s => new XElement("flag", new XAttribute("value", s.Trim()))));
                }
                else
                {
                    var val = Enum.GetName(cata.ArgumentType, cata.Value) ?? "";
                    yield return
                        new XElement("enum", new XAttribute("value", val));
                }
            }
            else if (cata.ArgumentType == typeof(Type))
            {
                XElement tmp = new XElement("tmp");
                DocGenerator.GenerateTypeRef(context.Clone(tmp), (Type)cata.Value, "value");
                yield return tmp.Attribute("value");
                foreach (XElement xElement in tmp.Elements())
                    yield return xElement;


// yield return new XAttribute("value", AssetIdentifier.FromMemberInfo((Type)cata.Value));
            }
            else
                yield return new XAttribute("value", cata.Value.ToString());
        }