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


C# ConstructorInfo.Invoke方法代码示例

本文整理汇总了C#中System.Reflection.ConstructorInfo.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# ConstructorInfo.Invoke方法的具体用法?C# ConstructorInfo.Invoke怎么用?C# ConstructorInfo.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.ConstructorInfo的用法示例。


在下文中一共展示了ConstructorInfo.Invoke方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetInstanceByConstructor

        private object GetInstanceByConstructor(ConstructorInfo constructorInfo, TypeConfig config)
        {
            ParameterInfo[] constructorParametersInfo = constructorInfo.GetParameters();
            object[] constructorParameters = new object[constructorParametersInfo.Length];
            object instance = null;
            var tuple = new Tuple<Type, Type>(config.TargetType, config.AttributeSelector);

            this.resolvingObjectsRepository.Add(tuple);

            for (int i = 0; i < constructorParameters.Length; i++)
            {
                object parameterValue = this.GetParameterValueFromPropertyInfo(constructorParametersInfo[i], config);

                if (parameterValue != null)
                {
                    constructorParameters[i] = parameterValue;
                }
                else
                {
                    Type parameterType = constructorParametersInfo[i].ParameterType;
                    Type colorAttributeType = this.GetColorAtributeFromParameter(constructorParametersInfo[i]);

                    constructorParameters[i] = this.GetInstance(parameterType, colorAttributeType);
                }
            }

            switch (config.Lifecycle)
            {
                case Lifecycle.PerRequest:
                    instance = this.resolvedObjectsRepository.ContainsKey(tuple) ? this.resolvedObjectsRepository[tuple] : constructorInfo.Invoke(constructorParameters);
                    break;
                case Lifecycle.Singleton:
                    if (this.singletonsRepository.ContainsKey(tuple))
                    {
                        instance = this.singletonsRepository[tuple];
                    }
                    else
                    {
                        instance = constructorInfo.Invoke(constructorParameters);
                        this.singletonsRepository.Add(tuple, instance);
                    }

                    break;
            }

            this.resolvingObjectsRepository.Remove(tuple);

            if (config.InitializeObjectWith != null)
            {
                config.InitializeObjectWith.Invoke(instance);
            }

            return instance;
        }
开发者ID:luismdcp,项目名称:PROMPT11-02-AdvancedProgramming.FinalWork,代码行数:54,代码来源:InstanceFactory.cs

示例2: Build

        public static IEntity Build(Mobile from, ConstructorInfo ctor, object[] values, string[,] props, PropertyInfo[] realProps, ref bool sendError)
        {
            object built = ctor.Invoke(values);

            if (built != null && realProps != null)
            {
                bool hadError = false;

                for (int i = 0; i < realProps.Length; ++i)
                {
                    if (realProps[i] == null)
                        continue;

                    string result = Properties.InternalSetValue(from, built, built, realProps[i], props[i, 1], props[i, 1], false);

                    if (result != "Property has been set.")
                    {
                        if (sendError)
                            from.SendMessage(result);

                        hadError = true;
                    }
                }

                if (hadError)
                    sendError = false;
            }

            return (IEntity)built;
        }
开发者ID:FreeReign,项目名称:forkuo,代码行数:30,代码来源:Add.cs

示例3: TestOneParameterForNull

        private void TestOneParameterForNull(ConstructorInfo constructor, object[] parameters, int parameterToTest, Type classUnderTest)
        {
            // copy parameters to not destroy them
            var parametersCopy = (object[])parameters.Clone();
            parametersCopy[parameterToTest] = null;

            var catched = false;

            try
            {
                constructor.Invoke(parametersCopy);
            }
            catch (TargetInvocationException e)
            {
                if (e.InnerException != null &&
                    e.InnerException.GetType() == typeof(ArgumentNullException))
                    catched = true;
            }

            if (!catched)
                _results.Add(new Weakness
                {
                    Type = classUnderTest,
                    Constructor = constructor,
                    ParameterPosition = parameterToTest + 1
                });
        }
开发者ID:PapaMufflon,项目名称:ConstructorTester,代码行数:27,代码来源:TypeTester.cs

示例4: ResolveWithDependencies

        public static object ResolveWithDependencies(this IEnumerable<ParameterInfo> parameterInfos, ConstructorInfo constructorInfo, ITypeResolver typeResolver)
        {
            var objects = new List<object>();

            objects.AddRange(parameterInfos.GetDependencies(typeResolver));

            return constructorInfo.Invoke(objects.ToArray());
        }
开发者ID:tylermercier,项目名称:dependency_injection_demo,代码行数:8,代码来源:ParameterInfoListExtension.cs

示例5: CallPageInjectionConstructor

        private static void CallPageInjectionConstructor(Control control, ConstructorInfo constructor)
        {
            var parameters = from parameter in constructor.GetParameters()
                             let parameterType = parameter.ParameterType
                             select Rest.Configuration.ServiceLocator.GetService(parameterType);

            constructor.Invoke(control, parameters.ToArray());
        }
开发者ID:dstarosta,项目名称:GitProjects,代码行数:8,代码来源:WebFormsInjectionHelper.cs

示例6: ConstructorInfoInvoke

 internal static object ConstructorInfoInvoke(ConstructorInfo ctor, object[] args)
 {
     Type declaringType = ctor.DeclaringType;
     if ((declaringType != null) && (!declaringType.IsVisible || !ctor.IsPublic))
     {
         DemandReflectionAccess(declaringType);
     }
     return ctor.Invoke(args);
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:9,代码来源:SecurityUtils.cs

示例7: FromMethodInfo

        private static Func<KeyValuePair<string, Task<ScenarioResult>>> FromMethodInfo(MethodInfo method, ConstructorInfo constructor)
        {
            return () =>
            {
                var instance = constructor.Invoke(new object[0]);

                return new KeyValuePair<string, Task<ScenarioResult>>(constructor.DeclaringType.FullName, (Task<ScenarioResult>) method.Invoke(instance, new object[0]));
            };
        }
开发者ID:dennisdoomen,项目名称:Cedar,代码行数:9,代码来源:FindScenarios.cs

示例8: Instantiate

		private static object Instantiate( ConstructorInfo constructor )
		{
			try
			{
				return constructor.Invoke( NoParameters );
			}
			catch( Exception e )
			{
				throw new InstantiationException( "could not instantiate test object", e, constructor.DeclaringType );
			}
		}
开发者ID:rcarrillopadron,项目名称:nhibernate-1.0.2.0,代码行数:11,代码来源:UnsavedValueFactory.cs

示例9: NewInstance

		public static object NewInstance(ConstructorInfo ctor, params object[] args)
		{
		
			try
			{
				return ctor.Invoke(args);
			} 
			catch(Exception e)
			{
				throw new Db4oException(e);
			}
		}
开发者ID:superyfwy,项目名称:db4o,代码行数:12,代码来源:ReflectPlatform.cs

示例10: ObjectFromConstructor

 public static object ObjectFromConstructor(ConstructorInfo C)
 {
     ParameterInfo[] ps = C.GetParameters();
     List<object> o = new List<object>();
     for (int i = 0; i < ps.Length; i++)
     {
         // if (ps[i].DefaultValue != null) o.Add(ps[i].DefaultValue); else
         if (ps[i].ParameterType.IsValueType) o.Add(Activator.CreateInstance(ps[i].ParameterType));
         else
             o.Add(ps[i].ParameterType.GetConstructor
                  (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null).Invoke(null));
     }
     return C.Invoke(o.ToArray());
 }
开发者ID:Martmath,项目名称:ISaveable,代码行数:14,代码来源:NewInstance.cs

示例11: GetInstance

        public override object GetInstance(ConstructorInfo constructor, object[] parameters = null)
        {
            var dependencies = constructor.GetParameters();
            if (dependencies.Count() == 0)
            {
                return Activator.CreateInstance(constructor.DeclaringType);
            }

            if (parameters == null || parameters.Count() != dependencies.Count())
            {
                throw new Exception("Incorrect number of parameters to invoke instance.");
            }

            return constructor.Invoke(parameters);
        }
开发者ID:stesta,项目名称:Injectamundo,代码行数:15,代码来源:TransientLifeStyle.cs

示例12: InvokeConstructorWithoutTargetInvocationException

        public static object InvokeConstructorWithoutTargetInvocationException(ConstructorInfo constructor, object[] args)
        {
            if (constructor == null)
                throw new ArgumentNullException("constructor");

            try
            {
                return constructor.Invoke(args);
            }
            catch (TargetInvocationException ex)
            {
                RethrowWithNoStackTraceLoss(ex.InnerException);
                throw;
            }
        }
开发者ID:dougrathbone,项目名称:mbunit-v3,代码行数:15,代码来源:ExceptionUtils.cs

示例13: CreateConstructor

		/// <summary>
		/// Creates a constructor method with no parameters for an object.
		/// </summary>
		/// <param name="type">Object type.</param>
		/// <param name="constructor">Constructor info used to create the function.</param>
		/// <returns>The object constructor.</returns>
		public static Constructor CreateConstructor(Type type, ConstructorInfo constructor) {
			#if UNITY_IOS
			
			return () => {
				return constructor.Invoke(null);
			};

			#else
			
			var method = new DynamicMethod(type.Name, type, null, type);
			ILGenerator generator = method.GetILGenerator();
			generator.Emit(OpCodes.Newobj, constructor);
			generator.Emit(OpCodes.Ret);
			return (Constructor)method.CreateDelegate(typeof(Constructor));

			#endif
		}
开发者ID:tomatell,项目名称:adic,代码行数:23,代码来源:MethodUtils.cs

示例14: CreateConstructorWithParams

		/// <summary>
		/// Creates a constructor method with parameters for an object of type <typeparamref name="T"/>.
		/// </summary>
		/// <param name="type">Object type.</param>
		/// <param name="constructor">Constructor info used to create the function.</param>
		/// <returns>The object constructor.</returns>
		public static ParamsConstructor CreateConstructorWithParams(Type type, ConstructorInfo constructor) {
			#if UNITY_IOS
			
			return (object[] parameters) => {
				return constructor.Invoke(parameters);
			};
			
			#else
			
			var parameters = constructor.GetParameters();
			
			Type[] parametersTypes = new Type[] { typeof(object[]) };
			var method = new DynamicMethod(type.Name, type, parametersTypes, type);
			ILGenerator generator = method.GetILGenerator();
			
			//Define parameters.
			for (int paramIndex = 0; paramIndex < parameters.Length; paramIndex++) {
				generator.Emit(OpCodes.Ldarg_0);
				
				//Define parameter position.
				switch (paramIndex) {
				case 0: generator.Emit(OpCodes.Ldc_I4_0); break;
				case 1: generator.Emit(OpCodes.Ldc_I4_1); break;
				case 2: generator.Emit(OpCodes.Ldc_I4_2); break;
				case 3: generator.Emit(OpCodes.Ldc_I4_3); break;
				case 4: generator.Emit(OpCodes.Ldc_I4_4); break;
				case 5: generator.Emit(OpCodes.Ldc_I4_5); break;
				case 6: generator.Emit(OpCodes.Ldc_I4_6); break;
				case 7: generator.Emit(OpCodes.Ldc_I4_7); break;
				case 8: generator.Emit(OpCodes.Ldc_I4_8); break;
				default: generator.Emit(OpCodes.Ldc_I4, paramIndex); break;
				}
				
				//Define parameter type.
				generator.Emit(OpCodes.Ldelem_Ref);
				Type paramType = parameters[paramIndex].ParameterType;
				generator.Emit(paramType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, paramType);
			}
			
			generator.Emit(OpCodes.Newobj, constructor);
			generator.Emit(OpCodes.Ret);
			return (ParamsConstructor)method.CreateDelegate(typeof(ParamsConstructor));
			
			#endif
		}
开发者ID:tomatell,项目名称:adic,代码行数:51,代码来源:MethodUtils.cs

示例15: Construct

 private static object Construct(DomainGenerator domaingenerator, ConstructorInfo constructor, int highestParameterCount)
 {
     var parameters = constructor.GetParameters();
     var parameterValues = new object[highestParameterCount];
     for (int i = 0; i < parameters.Length; i++)
     {
         var parmeterChoiceConvention = domaingenerator.choiceConventions.FirstOrDefault(c => c.Type == parameters[i].ParameterType);
         if (parmeterChoiceConvention != null)
         {
             var choice = parmeterChoiceConvention.Possibilities.PickOne();
             parameterValues.SetValue(choice, i);
             continue;
         }
         var primitiveGenerator = primitiveGenerators.Get(parameters[i].ParameterType);
         if (primitiveGenerator != null)
         {
             parameterValues.SetValue(primitiveGenerator.RandomAsObject(), i);
         }
     }
     return constructor.Invoke(parameterValues);
 }
开发者ID:Bunk,项目名称:QuickGenerate,代码行数:21,代码来源:Create.cs


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