Exemplo n.º 1
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);
        }