Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
{
public delegate object TypeActivationDelegate(object[] arguments);
if (constructor == null)
throw new
InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"Could not find
constructor matching signature {0}({1})", returnType.FullName, string.Join(",",
from argument in paramTypes
select argument.FullName)));
var constructorParams = constructor.GetParameters();
var method =
new DynamicMethod(
string.Format("{0}__{1}", constructor.DeclaringType.Name,
Guid.NewGuid().ToString().Replace("-", "")), typeof(object),
new[] { typeof(object[]) }, restrictedSkipVisibility);
ilGenerator.Emit(OpCodes.Ldarg_0);
switch (i)
{
case 0:
ilGenerator.Emit(OpCodes.Ldc_I4_0);
break;
case 1:
ilGenerator.Emit(OpCodes.Ldc_I4_1);
break;
case 2:
ilGenerator.Emit(OpCodes.Ldc_I4_2);
break;
case 3:
ilGenerator.Emit(OpCodes.Ldc_I4_3);
break;
case 4:
ilGenerator.Emit(OpCodes.Ldc_I4_4);
break;
case 5:
ilGenerator.Emit(OpCodes.Ldc_I4_5);
break;
case 6:
ilGenerator.Emit(OpCodes.Ldc_I4_6);
break;
case 7:
ilGenerator.Emit(OpCodes.Ldc_I4_7);
break;
case 8:
ilGenerator.Emit(OpCodes.Ldc_I4_8);
break;
default:
ilGenerator.Emit(OpCodes.Ldc_I4_S, i);
break;
}
ilGenerator.Emit(OpCodes.Ldelem_Ref);
ilGenerator.Emit(paramType.IsValueType ? OpCodes.Unbox_Any :
OpCodes.Castclass, paramType);
}
ilGenerator.Emit(OpCodes.Newobj, constructor);
ilGenerator.Emit(OpCodes.Ret);
return method;
}
private static DynamicMethod MakeCreationMethodTypeSafe(bool
restrictedSkipVisibility, Type returnType, params Type[] paramTypes)
{
var constructor = returnType.GetConstructor(paramTypes);
if (constructor == null)
throw new
InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"Could not find
constructor matching signature {0}({1})", returnType.FullName, string.Join(",",
from argument in paramTypes
select argument.FullName)));
var constructorParams = constructor.GetParameters();
var method =
new DynamicMethod(
string.Format("{0}__{1}", constructor.DeclaringType.Name,
Guid.NewGuid().ToString().Replace("-", "")), constructor.DeclaringType,
(from param in constructorParams select
param.ParameterType).ToArray(), restrictedSkipVisibility);
ilGenerator.Emit(OpCodes.Newobj, constructor);
ilGenerator.Emit(OpCodes.Ret);
return method;
}
returnType.FullName,
string.Join(",",
from param in paramTypes
select param.FullName)));
returnType.FullName,
string.Join(",",
from param in paramTypes
select param.FullName)));
if (delMethod.ReturnType != constructor.DeclaringType)
throw new InvalidOperationException("The return type of the
delegate must match the constructors declaring type");