private void Initialize (ConstructorInfo con, object [] constructorArgs, PropertyInfo [] namedProperties, object [] propertyValues, FieldInfo [] namedFields, object [] fieldValues) { 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 atype = ctor.DeclaringType; int i; i = 0; foreach (FieldInfo fi in namedFields) { Type t = fi.DeclaringType; if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t))) throw new ArgumentException ("Field '" + fi.Name + "' does not belong to the same class as the constructor"); if (!IsValidType (fi.FieldType)) throw new ArgumentException ("Field '" + fi.Name + "' does not have a valid type."); // FIXME: Check enums and TypeBuilders as well if (fieldValues [i] != null) // IsEnum does not seem to work on TypeBuilders if (!(fi.FieldType is TypeBuilder) && !fi.FieldType.IsEnum && !fi.FieldType.IsInstanceOfType (fieldValues [i])) { // // mcs allways uses object[] for array types and // MS.NET allows this // if (!fi.FieldType.IsArray) throw new ArgumentException ("Value of field '" + fi.Name + "' does not match field type: " + fi.FieldType); } i ++; } i = 0; foreach (PropertyInfo pi in namedProperties) { if (!pi.CanWrite) throw new ArgumentException ("Property '" + pi.Name + "' does not have a setter."); Type t = pi.DeclaringType; if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t))) throw new ArgumentException ("Property '" + pi.Name + "' does not belong to the same class as the constructor"); if (!IsValidType (pi.PropertyType)) throw new ArgumentException ("Property '" + pi.Name + "' does not have a valid type."); if (propertyValues [i] != null) { if (!(pi.PropertyType is TypeBuilder) && !pi.PropertyType.IsEnum && !pi.PropertyType.IsInstanceOfType (propertyValues [i])) if (!pi.PropertyType.IsArray) throw new ArgumentException ("Value of property '" + pi.Name + "' does not match property type: " + pi.PropertyType + " -> " + propertyValues [i]); } i ++; } i = 0; foreach (ParameterInfo pi in GetParameters (con)) { if (pi != null) { Type paramType = pi.ParameterType; if (!IsValidType (paramType)) throw new ArgumentException ("Parameter " + i + " does not have a valid type."); if (constructorArgs [i] != null) { if (!(paramType is TypeBuilder) && !paramType.IsEnum && !paramType.IsInstanceOfType (constructorArgs [i])) if (!paramType.IsArray) throw new ArgumentException ("Value of argument " + i + " does not match parameter type: " + paramType + " -> " + constructorArgs [i]); if (!IsValidParam (constructorArgs [i], paramType)) throw new ArgumentException ("Cannot emit a CustomAttribute with argument of type " + constructorArgs [i].GetType () + "."); } } i ++; } data = GetBlob (atype.Assembly, con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues); }