本文整理汇总了C#中System.Reflection.ConstructorInfo.GetParametersCount方法的典型用法代码示例。如果您正苦于以下问题:C# ConstructorInfo.GetParametersCount方法的具体用法?C# ConstructorInfo.GetParametersCount怎么用?C# ConstructorInfo.GetParametersCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.ConstructorInfo
的用法示例。
在下文中一共展示了ConstructorInfo.GetParametersCount方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Initialize
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.GetParametersCount () != 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.");
if (!IsValidValue (fi.FieldType, fieldValues [i]))
throw new ArgumentException ("Field " + fi.Name + " is not a valid value.");
// 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 (!IsValidValue (pi.PropertyType, propertyValues [i]))
throw new ArgumentException ("Property " + pi.Name + " is not a valid value.");
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 (!IsValidValue (paramType, constructorArgs [i]))
throw new ArgumentException ("Parameter " + i + " is not a valid value.");
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);
}