Exemplo n.º 1
0
        internal static UnmanagedMarshal get_umarshal(CustomAttributeBuilder customBuilder, bool is_field)
        {
            byte[]        array          = customBuilder.Data;
            UnmanagedType elemType       = (UnmanagedType)80;
            int           num            = -1;
            int           sizeParamIndex = -1;
            bool          flag           = false;
            string        text           = null;
            Type          typeref        = null;
            string        cookie         = string.Empty;
            int           num2           = (int)array[2];

            num2 |= (int)array[3] << 8;
            string fullName = CustomAttributeBuilder.GetParameters(customBuilder.Ctor)[0].ParameterType.FullName;
            int    num3     = 6;

            if (fullName == "System.Int16")
            {
                num3 = 4;
            }
            int num4 = (int)array[num3++];

            num4 |= (int)array[num3++] << 8;
            int i = 0;

            while (i < num4)
            {
                num3++;
                int num5 = (int)array[num3++];
                if (num5 == 85)
                {
                    int num6 = CustomAttributeBuilder.decode_len(array, num3, out num3);
                    CustomAttributeBuilder.string_from_bytes(array, num3, num6);
                    num3 += num6;
                }
                int    num7  = CustomAttributeBuilder.decode_len(array, num3, out num3);
                string text2 = CustomAttributeBuilder.string_from_bytes(array, num3, num7);
                num3 += num7;
                string text3 = text2;
                if (text3 != null)
                {
                    if (CustomAttributeBuilder.< > f__switch$map1C == null)
                    {
                        CustomAttributeBuilder.< > f__switch$map1C = new Dictionary <string, int>(9)
                        {
                            {
                                "ArraySubType",
                                0
                            },
                            {
                                "SizeConst",
                                1
                            },
                            {
                                "SafeArraySubType",
                                2
                            },
                            {
                                "IidParameterIndex",
                                3
                            },
                            {
                                "SafeArrayUserDefinedSubType",
                                4
                            },
                            {
                                "SizeParamIndex",
                                5
                            },
                            {
                                "MarshalType",
                                6
                            },
                            {
                                "MarshalTypeRef",
                                7
                            },
                            {
                                "MarshalCookie",
                                8
                            }
                        };
                    }
                    int num8;
                    if (CustomAttributeBuilder.< > f__switch$map1C.TryGetValue(text3, out num8))
                    {
                        switch (num8)
                        {
                        case 0:
                        {
                            int num9 = (int)array[num3++];
                            num9    |= (int)array[num3++] << 8;
                            num9    |= (int)array[num3++] << 16;
                            num9    |= (int)array[num3++] << 24;
                            elemType = (UnmanagedType)num9;
                            break;
                        }

                        case 1:
                        {
                            int num9 = (int)array[num3++];
                            num9 |= (int)array[num3++] << 8;
                            num9 |= (int)array[num3++] << 16;
                            num9 |= (int)array[num3++] << 24;
                            num   = num9;
                            flag  = true;
                            break;
                        }

                        case 2:
                        {
                            int num9 = (int)array[num3++];
                            num9    |= (int)array[num3++] << 8;
                            num9    |= (int)array[num3++] << 16;
                            num9    |= (int)array[num3++] << 24;
                            elemType = (UnmanagedType)num9;
                            break;
                        }

                        case 3:
                            num3 += 4;
                            break;

                        case 4:
                            num7 = CustomAttributeBuilder.decode_len(array, num3, out num3);
                            CustomAttributeBuilder.string_from_bytes(array, num3, num7);
                            num3 += num7;
                            break;

                        case 5:
                        {
                            int num9 = (int)array[num3++];
                            num9          |= (int)array[num3++] << 8;
                            sizeParamIndex = num9;
                            flag           = true;
                            break;
                        }

                        case 6:
                            num7  = CustomAttributeBuilder.decode_len(array, num3, out num3);
                            text  = CustomAttributeBuilder.string_from_bytes(array, num3, num7);
                            num3 += num7;
                            break;

                        case 7:
                            num7    = CustomAttributeBuilder.decode_len(array, num3, out num3);
                            text    = CustomAttributeBuilder.string_from_bytes(array, num3, num7);
                            typeref = Type.GetType(text);
                            num3   += num7;
                            break;

                        case 8:
                            num7   = CustomAttributeBuilder.decode_len(array, num3, out num3);
                            cookie = CustomAttributeBuilder.string_from_bytes(array, num3, num7);
                            num3  += num7;
                            break;

                        default:
                            goto IL_34F;
                        }
                        i++;
                        continue;
                    }
                }
IL_34F:
                throw new Exception("Unknown MarshalAsAttribute field: " + text2);
            }
            UnmanagedType unmanagedType = (UnmanagedType)num2;

            switch (unmanagedType)
            {
            case UnmanagedType.LPArray:
                if (flag)
                {
                    return(UnmanagedMarshal.DefineLPArrayInternal(elemType, num, sizeParamIndex));
                }
                return(UnmanagedMarshal.DefineLPArray(elemType));

            default:
                if (unmanagedType == UnmanagedType.SafeArray)
                {
                    return(UnmanagedMarshal.DefineSafeArray(elemType));
                }
                if (unmanagedType != UnmanagedType.ByValArray)
                {
                    if (unmanagedType != UnmanagedType.ByValTStr)
                    {
                        return(UnmanagedMarshal.DefineUnmanagedMarshal((UnmanagedType)num2));
                    }
                    return(UnmanagedMarshal.DefineByValTStr(num));
                }
                else
                {
                    if (!is_field)
                    {
                        throw new ArgumentException("Specified unmanaged type is only valid on fields");
                    }
                    return(UnmanagedMarshal.DefineByValArray(num));
                }
                break;

            case UnmanagedType.CustomMarshaler:
                return(UnmanagedMarshal.DefineCustom(typeref, cookie, text, Guid.Empty));
            }
        }
Exemplo n.º 2
0
        internal static CustomAttributeBuilder.CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBuilder)
        {
            byte[]          array           = customBuilder.Data;
            ConstructorInfo constructorInfo = customBuilder.Ctor;
            int             num             = 0;

            CustomAttributeBuilder.CustomAttributeInfo result = default(CustomAttributeBuilder.CustomAttributeInfo);
            if (array.Length < 2)
            {
                throw new Exception("Custom attr length is only '" + array.Length + "'");
            }
            if (array[0] != 1 || array[1] != 0)
            {
                throw new Exception("Prolog invalid");
            }
            num = 2;
            ParameterInfo[] parameters = CustomAttributeBuilder.GetParameters(constructorInfo);
            result.ctor     = constructorInfo;
            result.ctorArgs = new object[parameters.Length];
            for (int i = 0; i < parameters.Length; i++)
            {
                result.ctorArgs[i] = CustomAttributeBuilder.decode_cattr_value(parameters[i].ParameterType, array, num, out num);
            }
            int num2 = (int)array[num] + (int)array[num + 1] * 256;

            num += 2;
            result.namedParamNames  = new string[num2];
            result.namedParamValues = new object[num2];
            for (int j = 0; j < num2; j++)
            {
                int    num3 = (int)array[num++];
                int    num4 = (int)array[num++];
                string text = null;
                if (num4 == 85)
                {
                    int num5 = CustomAttributeBuilder.decode_len(array, num, out num);
                    text = CustomAttributeBuilder.string_from_bytes(array, num, num5);
                    num += num5;
                }
                int    num6  = CustomAttributeBuilder.decode_len(array, num, out num);
                string text2 = CustomAttributeBuilder.string_from_bytes(array, num, num6);
                result.namedParamNames[j] = text2;
                num += num6;
                if (num3 != 83)
                {
                    throw new Exception("Unknown named type: " + num3);
                }
                FieldInfo field = constructorInfo.DeclaringType.GetField(text2, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (field == null)
                {
                    throw new Exception(string.Concat(new object[]
                    {
                        "Custom attribute type '",
                        constructorInfo.DeclaringType,
                        "' doesn't contain a field named '",
                        text2,
                        "'"
                    }));
                }
                object obj = CustomAttributeBuilder.decode_cattr_value(field.FieldType, array, num, out num);
                if (text != null)
                {
                    Type type = Type.GetType(text);
                    obj = Enum.ToObject(type, obj);
                }
                result.namedParamValues[j] = obj;
            }
            return(result);
        }
Exemplo n.º 3
0
        private void Initialize(ConstructorInfo con, object[] constructorArgs, PropertyInfo[] namedProperties, object[] propertyValues, FieldInfo[] namedFields, object[] fieldValues)
        {
            this.ctor = con;
            if (con == null)
            {
                throw new ArgumentNullException("con");
            }
            if (constructorArgs == null)
            {
                throw new ArgumentNullException("constructorArgs");
            }
            if (namedProperties == null)
            {
                throw new ArgumentNullException("namedProperties");
            }
            if (propertyValues == null)
            {
                throw new ArgumentNullException("propertyValues");
            }
            if (namedFields == null)
            {
                throw new ArgumentNullException("namedFields");
            }
            if (fieldValues == null)
            {
                throw new ArgumentNullException("fieldValues");
            }
            if (con.GetParameterCount() != constructorArgs.Length)
            {
                throw new ArgumentException("Parameter count does not match passed in argument value count.");
            }
            if (namedProperties.Length != propertyValues.Length)
            {
                throw new ArgumentException("Array lengths must be the same.", "namedProperties, propertyValues");
            }
            if (namedFields.Length != fieldValues.Length)
            {
                throw new ArgumentException("Array lengths must be the same.", "namedFields, fieldValues");
            }
            if ((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static || (con.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
            {
                throw new ArgumentException("Cannot have private or static constructor.");
            }
            Type declaringType = this.ctor.DeclaringType;
            int  num           = 0;

            foreach (FieldInfo fieldInfo in namedFields)
            {
                Type declaringType2 = fieldInfo.DeclaringType;
                if (!this.IsValidType(declaringType2))
                {
                    throw new ArgumentException("Field '" + fieldInfo.Name + "' does not have a valid type.");
                }
                if (declaringType != declaringType2 && !declaringType2.IsSubclassOf(declaringType) && !declaringType.IsSubclassOf(declaringType2))
                {
                    throw new ArgumentException("Field '" + fieldInfo.Name + "' does not belong to the same class as the constructor");
                }
                if (fieldValues[num] != null && !(fieldInfo.FieldType is TypeBuilder) && !fieldInfo.FieldType.IsEnum && !fieldInfo.FieldType.IsInstanceOfType(fieldValues[num]) && !fieldInfo.FieldType.IsArray)
                {
                    throw new ArgumentException(string.Concat(new object[]
                    {
                        "Value of field '",
                        fieldInfo.Name,
                        "' does not match field type: ",
                        fieldInfo.FieldType
                    }));
                }
                num++;
            }
            num = 0;
            foreach (PropertyInfo propertyInfo in namedProperties)
            {
                if (!propertyInfo.CanWrite)
                {
                    throw new ArgumentException("Property '" + propertyInfo.Name + "' does not have a setter.");
                }
                Type declaringType3 = propertyInfo.DeclaringType;
                if (!this.IsValidType(declaringType3))
                {
                    throw new ArgumentException("Property '" + propertyInfo.Name + "' does not have a valid type.");
                }
                if (declaringType != declaringType3 && !declaringType3.IsSubclassOf(declaringType) && !declaringType.IsSubclassOf(declaringType3))
                {
                    throw new ArgumentException("Property '" + propertyInfo.Name + "' does not belong to the same class as the constructor");
                }
                if (propertyValues[num] != null && !(propertyInfo.PropertyType is TypeBuilder) && !propertyInfo.PropertyType.IsEnum && !propertyInfo.PropertyType.IsInstanceOfType(propertyValues[num]) && !propertyInfo.PropertyType.IsArray)
                {
                    throw new ArgumentException(string.Concat(new object[]
                    {
                        "Value of property '",
                        propertyInfo.Name,
                        "' does not match property type: ",
                        propertyInfo.PropertyType,
                        " -> ",
                        propertyValues[num]
                    }));
                }
                num++;
            }
            num = 0;
            foreach (ParameterInfo parameterInfo in CustomAttributeBuilder.GetParameters(con))
            {
                if (parameterInfo != null)
                {
                    Type parameterType = parameterInfo.ParameterType;
                    if (!this.IsValidType(parameterType))
                    {
                        throw new ArgumentException("Argument " + num + " does not have a valid type.");
                    }
                    if (constructorArgs[num] != null && !(parameterType is TypeBuilder) && !parameterType.IsEnum && !parameterType.IsInstanceOfType(constructorArgs[num]) && !parameterType.IsArray)
                    {
                        throw new ArgumentException(string.Concat(new object[]
                        {
                            "Value of argument ",
                            num,
                            " does not match parameter type: ",
                            parameterType,
                            " -> ",
                            constructorArgs[num]
                        }));
                    }
                }
                num++;
            }
            this.data = CustomAttributeBuilder.GetBlob(declaringType.Assembly, con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
        }