本文整理汇总了C#中System.Reflection.ConstructorInfo类的典型用法代码示例。如果您正苦于以下问题:C# ConstructorInfo类的具体用法?C# ConstructorInfo怎么用?C# ConstructorInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConstructorInfo类属于System.Reflection命名空间,在下文中一共展示了ConstructorInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToDescription
public static string ToDescription(ConstructorInfo constructor)
{
var parameters = constructor.GetParameters();
var paramList = parameters.Select(x => {
if (x.ParameterType.IsSimple())
{
return "{0} {1}".ToFormat(x.ParameterType.GetTypeName(), x.Name);
}
else
{
if (parameters.Where(p => p.ParameterType == x.ParameterType).Count() > 1)
{
return "{0} {1}".ToFormat(x.ParameterType.GetTypeName(), x.Name);
}
else
{
return x.ParameterType.GetTypeName();
}
}
}).ToArray();
return "new {0}({1})".ToFormat(constructor.DeclaringType.GetTypeName(), string.Join(", ", paramList));
}
示例2: CustomAttributeData
internal CustomAttributeData(Module scope, CustomAttributeRecord caRecord)
{
this.m_scope = scope;
this.m_ctor = (ConstructorInfo) RuntimeType.GetMethodBase(scope, (int) caRecord.tkCtor);
ParameterInfo[] parametersNoCopy = this.m_ctor.GetParametersNoCopy();
this.m_ctorParams = new CustomAttributeCtorParameter[parametersNoCopy.Length];
for (int i = 0; i < parametersNoCopy.Length; i++)
{
this.m_ctorParams[i] = new CustomAttributeCtorParameter(InitCustomAttributeType(parametersNoCopy[i].ParameterType, scope));
}
FieldInfo[] fields = this.m_ctor.DeclaringType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] properties = this.m_ctor.DeclaringType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
this.m_namedParams = new CustomAttributeNamedParameter[properties.Length + fields.Length];
for (int j = 0; j < fields.Length; j++)
{
this.m_namedParams[j] = new CustomAttributeNamedParameter(fields[j].Name, CustomAttributeEncoding.Field, InitCustomAttributeType(fields[j].FieldType, scope));
}
for (int k = 0; k < properties.Length; k++)
{
this.m_namedParams[k + fields.Length] = new CustomAttributeNamedParameter(properties[k].Name, CustomAttributeEncoding.Property, InitCustomAttributeType(properties[k].PropertyType, scope));
}
this.m_members = new MemberInfo[fields.Length + properties.Length];
fields.CopyTo(this.m_members, 0);
properties.CopyTo(this.m_members, fields.Length);
CustomAttributeEncodedArgument.ParseAttributeArguments(caRecord.blob, ref this.m_ctorParams, ref this.m_namedParams, this.m_scope);
}
示例3: IsBetterChoice
private bool IsBetterChoice(ConstructorInfo current, ConstructorInfo candidate)
{
if (candidate.GetParameters().Any(x => x.ParameterType.IsSealed))
return false;
return current == null || current.GetParameters().Length < candidate.GetParameters().Length;
}
示例4: NewExpression
internal NewExpression (ConstructorInfo constructor, ReadOnlyCollection<Expression> arguments, ReadOnlyCollection<MemberInfo> members)
: base (ExpressionType.New, constructor.DeclaringType)
{
this.constructor = constructor;
this.arguments = arguments;
this.members = members;
}
示例5: GetParameterCount
/// <summary>
/// Gets the number of parameters that the system recognizes
/// </summary>
/// <param name="Constructor">Constructor to check</param>
/// <param name="MappingManager">Mapping manager</param>
/// <returns>The number of parameters that it has knoweledge of</returns>
private static int GetParameterCount(ConstructorInfo Constructor, MappingManager MappingManager)
{
int Count = 0;
ParameterInfo[] Parameters = Constructor.GetParameters();
foreach (ParameterInfo Parameter in Parameters)
{
bool Inject = true;
object[] Attributes = Parameter.GetCustomAttributes(false);
if (Attributes.Length > 0)
{
foreach (Attribute Attribute in Attributes)
{
if (MappingManager.GetMapping(Parameter.ParameterType, Attribute.GetType()) != null)
{
++Count;
Inject = false;
break;
}
}
}
if (Inject)
{
if (MappingManager.GetMapping(Parameter.ParameterType) != null)
++Count;
}
}
if (Count == Parameters.Length)
return Count;
return int.MinValue;
}
示例6: AliasToBeanResultTransformer
public AliasToBeanResultTransformer(System.Type resultClass)
{
if (resultClass == null)
{
throw new ArgumentNullException("resultClass");
}
this.resultClass = resultClass;
constructor = resultClass.GetConstructor(flags, null, System.Type.EmptyTypes, null);
// if resultClass is a ValueType (struct), GetConstructor will return null...
// in that case, we'll use Activator.CreateInstance instead of the ConstructorInfo to create instances
if (constructor == null && resultClass.IsClass)
{
throw new ArgumentException("The target class of a AliasToBeanResultTransformer need a parameter-less constructor",
"resultClass");
}
propertyAccessor =
new ChainedPropertyAccessor(new[]
{
PropertyAccessorFactory.GetPropertyAccessor(null),
PropertyAccessorFactory.GetPropertyAccessor("field")
});
}
示例7: Check
public void Check(ConstructorInfo info, Dependency parent)
{
if (info.IsPrivate && !info.ReflectedType.IsAbstract)
{
parent.Add(new ProblemDependency(string.Format("This is a private constructor")));
}
}
开发者ID:royosherove,项目名称:dotnet-depender,代码行数:7,代码来源:FindPrivateConstructorsOnNonAbstractClassesRule.cs
示例8: ConstructorMap
public ConstructorMap(ConstructorInfo ctor, IEnumerable<ConstructorParameterMap> ctorParams)
{
Ctor = ctor;
CtorParams = ctorParams;
_runtimeCtor = DelegateFactory.CreateCtor(ctor, CtorParams);
}
示例9: GetConstructor
public ConstructorMethod GetConstructor(ConstructorInfo constructor)
{
DynamicMethod dynamicConstructor = CreateDynamicConstructor(constructor);
ILGenerator il = dynamicConstructor.GetILGenerator();
ParameterInfo[] parameters = constructor.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
il.Emit(OpCodes.Ldarg_0);
switch (i)
{
case 0: il.Emit(OpCodes.Ldc_I4_0); break;
case 1: il.Emit(OpCodes.Ldc_I4_1); break;
case 2: il.Emit(OpCodes.Ldc_I4_2); break;
case 3: il.Emit(OpCodes.Ldc_I4_3); break;
case 4: il.Emit(OpCodes.Ldc_I4_4); break;
case 5: il.Emit(OpCodes.Ldc_I4_5); break;
case 6: il.Emit(OpCodes.Ldc_I4_6); break;
case 7: il.Emit(OpCodes.Ldc_I4_7); break;
case 8: il.Emit(OpCodes.Ldc_I4_8); break;
default: il.Emit(OpCodes.Ldc_I4, i); break;
}
il.Emit(OpCodes.Ldelem_Ref);
Type paramType = parameters[i].ParameterType;
il.Emit(paramType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, paramType);
}
il.Emit(OpCodes.Newobj, constructor);
il.BoxIfNeeded(constructor.DeclaringType);
il.Emit(OpCodes.Ret);
return (ConstructorMethod)dynamicConstructor.CreateDelegate(typeof(ConstructorMethod));
}
示例10: ResolveConstructorArguments
private static ResolvedConstructor ResolveConstructorArguments(ConstructorInfo constructor, IDummyValueCreationSession session)
{
Logger.Debug("Beginning to resolve constructor with {0} arguments.", constructor.GetParameters().Length);
var resolvedArguments = new List<ResolvedArgument>();
foreach (var argument in constructor.GetParameters())
{
object result = null;
var resolvedArgument = new ResolvedArgument
{
WasResolved = session.TryResolveDummyValue(argument.ParameterType, out result),
ResolvedValue = result,
ArgumentType = argument.ParameterType
};
Logger.Debug("Was able to resolve {0}: {1}.", argument.ParameterType, resolvedArgument.WasResolved);
resolvedArguments.Add(resolvedArgument);
}
return new ResolvedConstructor
{
Arguments = resolvedArguments.ToArray()
};
}
示例11: GetCtorArgumentsAndBaseCtorToCall
protected override ArgumentReference[] GetCtorArgumentsAndBaseCtorToCall(Type targetFieldType, ProxyGenerationOptions proxyGenerationOptions,out ConstructorInfo baseConstructor)
{
if (proxyGenerationOptions.Selector == null)
{
baseConstructor = InvocationMethods.CompositionInvocationConstructorNoSelector;
return new[]
{
new ArgumentReference(targetFieldType),
new ArgumentReference(typeof(object)),
new ArgumentReference(typeof(IInterceptor[])),
new ArgumentReference(typeof(MethodInfo)),
new ArgumentReference(typeof(object[])),
};
}
baseConstructor = InvocationMethods.CompositionInvocationConstructorWithSelector;
return new[]
{
new ArgumentReference(targetFieldType),
new ArgumentReference(typeof(object)),
new ArgumentReference(typeof(IInterceptor[])),
new ArgumentReference(typeof(MethodInfo)),
new ArgumentReference(typeof(object[])),
new ArgumentReference(typeof(IInterceptorSelector)),
new ArgumentReference(typeof(IInterceptor[]).MakeByRefType())
};
}
示例12: DemandMemberAccessIfNeeded
private static void DemandMemberAccessIfNeeded(ConstructorInfo constructor)
{
if (!constructor.IsVisible())
{
DemandMemberAccess(constructor);
}
}
示例13: InterfaceObject
internal InterfaceObject(Type tp) : base(tp) {
CoClassAttribute coclass = (CoClassAttribute)
Attribute.GetCustomAttribute(tp, cc_attr);
if (coclass != null) {
ctor = coclass.CoClass.GetConstructor(Type.EmptyTypes);
}
}
示例14: VisitNew
protected override Expression VisitNew(NewExpression node)
{
var originalConstructorParameters = node.Arguments.Select(x => x.Type).ToList();
var genericType = node.Constructor.DeclaringType.GetGenericTypeDefinition();
var genericTypeParameters = genericType.GetTypeInfo().GenericTypeArguments.ToList();
var originalGenericArgs = node.Constructor.DeclaringType.GetTypeInfo().GenericTypeArguments.ToList();
if (genericTypeParameters.Count != _newGenericArgs.Length)
{
throw new InvalidOperationException("Wrong number of generic argument values specified. This type requires " + genericTypeParameters.Count + " generic type arguments");
}
for (int i = 0; i < _newGenericArgs.Length; i++)
{
var constraints = genericTypeParameters[i].GetTypeInfo().GetGenericParameterConstraints().ToList();
foreach (var constraint in constraints)
{
if (!constraint.GetTypeInfo().IsAssignableFrom(_newGenericArgs[i].GetTypeInfo()))
{
throw new InvalidOperationException("Generic type parameter " + i + " is constrained to type " + constraint + ". The supplied type " + _newGenericArgs[i] + " does not meet this constraint");
}
}
}
result = genericType.MakeGenericType(_newGenericArgs.ToArray()).GetTypeInfo().DeclaredConstructors.Where(x => x == node.Constructor).Single();
throw new VisitStoppedException();
}
示例15: NullableMembers
public NullableMembers(Type elementType)
{
NullableType = NullableTypeDefinition.MakeGenericType(elementType);
Constructor = NullableType.GetConstructor(new[] { elementType });
GetHasValue = NullableType.GetProperty("HasValue").GetGetMethod();
GetValue = NullableType.GetProperty("Value").GetGetMethod();
}