当前位置: 首页>>代码示例>>C#>>正文


C# Reflection.ConstructorInfo类代码示例

本文整理汇总了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));
        }
开发者ID:Kingefosa,项目名称:structuremap,代码行数:25,代码来源:ConstructorStep.cs

示例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);
 }
开发者ID:randomize,项目名称:VimConfig,代码行数:26,代码来源:CustomAttributeData.cs

示例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;
        }
开发者ID:gregkowalski,项目名称:Moq.AutoMocker,代码行数:7,代码来源:ConstructorSelector.cs

示例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;
		}
开发者ID:superyfwy,项目名称:db4o,代码行数:7,代码来源:NewExpression.cs

示例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;
 }
开发者ID:jerrymds,项目名称:Concord,代码行数:36,代码来源:ConstructorList.cs

示例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")
				                            	});
		}
开发者ID:Ruhollah,项目名称:nhibernate-core,代码行数:25,代码来源:AliasToBeanResultTransformer.cs

示例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);
        }
开发者ID:sclcwwl,项目名称:Gimela,代码行数:7,代码来源:ConstructorMap.cs

示例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));
        }
开发者ID:soxtoby,项目名称:ForSerial,代码行数:31,代码来源:DynamicMethodProvider.cs

示例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()
                       };
        }
开发者ID:jszumigaj,项目名称:FakeItEasy,代码行数:26,代码来源:FakeObjectCreator.cs

示例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())
			};
		}
开发者ID:JulianBirch,项目名称:Castle.Core,代码行数:27,代码来源:InterfaceInvocationTypeGenerator.cs

示例12: DemandMemberAccessIfNeeded

 private static void DemandMemberAccessIfNeeded(ConstructorInfo constructor)
 {
     if (!constructor.IsVisible())
     {
         DemandMemberAccess(constructor);
     }
 }
开发者ID:nlhepler,项目名称:mono,代码行数:7,代码来源:ReflectionInvoke.cs

示例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);
     }
 }
开发者ID:JackHang,项目名称:Wox,代码行数:7,代码来源:interfaceobject.cs

示例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();
        }
开发者ID:DanMannMann,项目名称:Reflekt,代码行数:26,代码来源:ConstructorBuilderVisitor.cs

示例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();
 }
开发者ID:jaygumji,项目名称:EnigmaDb,代码行数:7,代码来源:NullableMembers.cs


注:本文中的System.Reflection.ConstructorInfo类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。