Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ CustomAttributeBuilder() [4/4]

System.Reflection.Emit.CustomAttributeBuilder.CustomAttributeBuilder ( ConstructorInfo con,
object?[] constructorArgs,
PropertyInfo[] namedProperties,
object?[] propertyValues,
FieldInfo[] namedFields,
object?[] fieldValues )
inline

Definition at line 30 of file CustomAttributeBuilder.cs.

31 {
32 if (con == null)
33 {
34 throw new ArgumentNullException("con");
35 }
36 if (constructorArgs == null)
37 {
38 throw new ArgumentNullException("constructorArgs");
39 }
40 if (namedProperties == null)
41 {
42 throw new ArgumentNullException("namedProperties");
43 }
44 if (propertyValues == null)
45 {
46 throw new ArgumentNullException("propertyValues");
47 }
48 if (namedFields == null)
49 {
50 throw new ArgumentNullException("namedFields");
51 }
52 if (fieldValues == null)
53 {
54 throw new ArgumentNullException("fieldValues");
55 }
56 if (namedProperties.Length != propertyValues.Length)
57 {
58 throw new ArgumentException(SR.Arg_ArrayLengthsDiffer, "namedProperties, propertyValues");
59 }
60 if (namedFields.Length != fieldValues.Length)
61 {
62 throw new ArgumentException(SR.Arg_ArrayLengthsDiffer, "namedFields, fieldValues");
63 }
64 if ((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static || (con.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
65 {
66 throw new ArgumentException(SR.Argument_BadConstructor);
67 }
68 if ((con.CallingConvention & CallingConventions.Standard) != CallingConventions.Standard)
69 {
70 throw new ArgumentException(SR.Argument_BadConstructorCallConv);
71 }
72 m_con = con;
73 m_constructorArgs = new object[constructorArgs.Length];
74 Array.Copy(constructorArgs, m_constructorArgs, constructorArgs.Length);
75 Type[] parameterTypes = con.GetParameterTypes();
76 if (parameterTypes.Length != constructorArgs.Length)
77 {
78 throw new ArgumentException(SR.Argument_BadParameterCountsForConstructor);
79 }
80 for (int i = 0; i < parameterTypes.Length; i++)
81 {
82 if (!ValidateType(parameterTypes[i]))
83 {
84 throw new ArgumentException(SR.Argument_BadTypeInCustomAttribute);
85 }
86 }
87 for (int i = 0; i < parameterTypes.Length; i++)
88 {
89 object obj = constructorArgs[i];
90 if (obj == null)
91 {
92 if (parameterTypes[i].IsValueType)
93 {
94 throw new ArgumentNullException($"{"constructorArgs"}[{i}]");
95 }
96 }
97 else
98 {
99 VerifyTypeAndPassedObjectType(parameterTypes[i], obj.GetType(), $"{"constructorArgs"}[{i}]");
100 }
101 }
102 MemoryStream output = new MemoryStream();
103 BinaryWriter binaryWriter = new BinaryWriter(output);
104 binaryWriter.Write((ushort)1);
105 for (int i = 0; i < constructorArgs.Length; i++)
106 {
107 EmitValue(binaryWriter, parameterTypes[i], constructorArgs[i]);
108 }
109 binaryWriter.Write((ushort)(namedProperties.Length + namedFields.Length));
110 for (int i = 0; i < namedProperties.Length; i++)
111 {
112 PropertyInfo propertyInfo = namedProperties[i];
113 if (propertyInfo == null)
114 {
115 throw new ArgumentNullException("namedProperties[" + i + "]");
116 }
117 Type propertyType = propertyInfo.PropertyType;
118 object obj2 = propertyValues[i];
119 if (obj2 == null && propertyType.IsValueType)
120 {
121 throw new ArgumentNullException("propertyValues[" + i + "]");
122 }
123 if (!ValidateType(propertyType))
124 {
125 throw new ArgumentException(SR.Argument_BadTypeInCustomAttribute);
126 }
127 if (!propertyInfo.CanWrite)
128 {
129 throw new ArgumentException(SR.Argument_NotAWritableProperty);
130 }
131 if (propertyInfo.DeclaringType != con.DeclaringType && !(con.DeclaringType is TypeBuilderInstantiation) && !con.DeclaringType.IsSubclassOf(propertyInfo.DeclaringType) && !TypeBuilder.IsTypeEqual(propertyInfo.DeclaringType, con.DeclaringType) && (!(propertyInfo.DeclaringType is TypeBuilder) || !con.DeclaringType.IsSubclassOf(((TypeBuilder)propertyInfo.DeclaringType).BakedRuntimeType)))
132 {
133 throw new ArgumentException(SR.Argument_BadPropertyForConstructorBuilder);
134 }
135 if (obj2 != null)
136 {
137 VerifyTypeAndPassedObjectType(propertyType, obj2.GetType(), $"{"propertyValues"}[{i}]");
138 }
139 binaryWriter.Write((byte)84);
140 EmitType(binaryWriter, propertyType);
141 EmitString(binaryWriter, namedProperties[i].Name);
142 EmitValue(binaryWriter, propertyType, obj2);
143 }
144 for (int i = 0; i < namedFields.Length; i++)
145 {
146 FieldInfo fieldInfo = namedFields[i];
147 if (fieldInfo == null)
148 {
149 throw new ArgumentNullException("namedFields[" + i + "]");
150 }
151 Type fieldType = fieldInfo.FieldType;
152 object obj3 = fieldValues[i];
153 if (obj3 == null && fieldType.IsValueType)
154 {
155 throw new ArgumentNullException("fieldValues[" + i + "]");
156 }
157 if (!ValidateType(fieldType))
158 {
159 throw new ArgumentException(SR.Argument_BadTypeInCustomAttribute);
160 }
161 if (fieldInfo.DeclaringType != con.DeclaringType && !(con.DeclaringType is TypeBuilderInstantiation) && !con.DeclaringType.IsSubclassOf(fieldInfo.DeclaringType) && !TypeBuilder.IsTypeEqual(fieldInfo.DeclaringType, con.DeclaringType) && (!(fieldInfo.DeclaringType is TypeBuilder) || !con.DeclaringType.IsSubclassOf(((TypeBuilder)namedFields[i].DeclaringType).BakedRuntimeType)))
162 {
163 throw new ArgumentException(SR.Argument_BadFieldForConstructorBuilder);
164 }
165 if (obj3 != null)
166 {
167 VerifyTypeAndPassedObjectType(fieldType, obj3.GetType(), $"{"fieldValues"}[{i}]");
168 }
169 binaryWriter.Write((byte)83);
170 EmitType(binaryWriter, fieldType);
171 EmitString(binaryWriter, fieldInfo.Name);
172 EmitValue(binaryWriter, fieldType, obj3);
173 }
174 m_blob = ((MemoryStream)binaryWriter.BaseStream).ToArray();
175 }
virtual Stream BaseStream
virtual void Write(bool value)
static void EmitType(BinaryWriter writer, Type type)
static void EmitString(BinaryWriter writer, string str)
static void EmitValue(BinaryWriter writer, Type type, object value)
static void VerifyTypeAndPassedObjectType(Type type, Type passedType, string paramName)

References System.SR.Arg_ArrayLengthsDiffer, System.SR.Argument_BadConstructor, System.SR.Argument_BadConstructorCallConv, System.SR.Argument_BadFieldForConstructorBuilder, System.SR.Argument_BadParameterCountsForConstructor, System.SR.Argument_BadPropertyForConstructorBuilder, System.SR.Argument_BadTypeInCustomAttribute, System.SR.Argument_NotAWritableProperty, System.Reflection.MethodBase.Attributes, System.IO.BinaryWriter.BaseStream, System.Reflection.MethodBase.CallingConvention, System.Reflection.PropertyInfo.CanWrite, System.Array.Copy(), System.Reflection.MemberInfo.DeclaringType, System.Reflection.Emit.CustomAttributeBuilder.EmitString(), System.Reflection.Emit.CustomAttributeBuilder.EmitType(), System.Reflection.Emit.CustomAttributeBuilder.EmitValue(), System.Reflection.FieldInfo.FieldType, System.Reflection.MethodBase.GetParameterTypes(), System.Type.GetType(), System.Type.IsSubclassOf(), System.Reflection.Emit.TypeBuilder.IsTypeEqual(), System.Type.IsValueType, System.Reflection.Emit.CustomAttributeBuilder.m_blob, System.Reflection.Emit.CustomAttributeBuilder.m_con, System.Reflection.Emit.CustomAttributeBuilder.m_constructorArgs, System.Reflection.MemberInfo.Name, System.Reflection.Name, System.obj, System.Reflection.PropertyInfo.PropertyType, System.Reflection.Emit.CustomAttributeBuilder.ValidateType(), System.Reflection.Emit.CustomAttributeBuilder.VerifyTypeAndPassedObjectType(), and System.IO.BinaryWriter.Write().