本文整理汇总了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;
}
示例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;
}
示例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
});
}
示例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());
}
示例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());
}
示例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);
}
示例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]));
};
}
示例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 );
}
}
示例9: NewInstance
public static object NewInstance(ConstructorInfo ctor, params object[] args)
{
try
{
return ctor.Invoke(args);
}
catch(Exception e)
{
throw new Db4oException(e);
}
}
示例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());
}
示例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);
}
示例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;
}
}
示例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
}
示例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
}
示例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);
}