本文整理汇总了C#中System.Reflection.Emit.ConstructorBuilder.GetILGenerator方法的典型用法代码示例。如果您正苦于以下问题:C# ConstructorBuilder.GetILGenerator方法的具体用法?C# ConstructorBuilder.GetILGenerator怎么用?C# ConstructorBuilder.GetILGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.ConstructorBuilder
的用法示例。
在下文中一共展示了ConstructorBuilder.GetILGenerator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitConstrucorIL
public void EmitConstrucorIL(ConstructorBuilder builder)
{
ConstructorInfo conObj = typeof(object).GetConstructor(new Type[0]);
//call constructor of base object
ILGenerator il = builder.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, conObj);
il.Emit(OpCodes.Ret);
}
示例2: GetInitConstructorIL
protected ILGenerator GetInitConstructorIL(ConstructorBuilder constructorBuilder, Dictionary<TypeFactoryMap, FieldBuilder> singletonFields)
{
var constrIl = constructorBuilder.GetILGenerator();
constrIl.Emit(OpCodes.Ldarg_0);
constrIl.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
foreach (var fieldPair in singletonFields)
{
var injectableGetSingletonMethod = typeof(IInjectableModule).GetMethod("GetSingleton").MakeGenericMethod(fieldPair.Key.BaseType);
constrIl.Emit(OpCodes.Ldarg_0);
constrIl.Emit(OpCodes.Ldarg_1);
constrIl.Emit(OpCodes.Ldstr, fieldPair.Key.Name);
constrIl.Emit(OpCodes.Callvirt, injectableGetSingletonMethod);
constrIl.Emit(OpCodes.Stfld, fieldPair.Value);
}
return constrIl;
}
示例3: Execute
public void Execute()
{
constructorBuilder = builder().TypeBuilder.DefineConstructor(
MethodAttributes.Public |
MethodAttributes.SpecialName |
MethodAttributes.RTSpecialName,
CallingConventions.Standard,
new Type[0]);
this.constructor = typeof(object).GetConstructor(new Type[0]);
ILGenerator il = constructorBuilder.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, constructor);
il.Emit(OpCodes.Ret);
}
示例4: PrepareSelf
/// <summary>
/// Creates a ConstructorBuilder for current constructor entity.
/// </summary>
public override void PrepareSelf()
{
// todo: remove when we support static ctors
if(IsStatic)
throw new LensCompilerException(CompilerMessages.ConstructorStatic);
if (ConstructorBuilder != null || IsImported)
return;
var ctx = ContainerType.Context;
if (ArgumentTypes == null)
ArgumentTypes = Arguments == null
? new Type[0]
: Arguments.Values.Select(fa => fa.GetArgumentType(ctx)).ToArray();
ConstructorBuilder = ContainerType.TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, ArgumentTypes);
Generator = ConstructorBuilder.GetILGenerator(Context.ILStreamSize);
}
示例5: OnCompile
protected internal override void OnCompile()
{
Builder = this.TargetClass.Builder.DefineConstructor(this.Attributes
, this.CallingConvention, this.ParameterTypes
);
ILGenerator il = SetILGenerator(Builder.GetILGenerator());
try
{
CompileParameters(Builder);
CompileLocals(il);
ILHelper msil = new ILHelper(il);
base.EmitInstructions(msil);
msil.Return();
}
finally
{
SetILGenerator(il);
}
}
示例6: EmitCtor
private void EmitCtor(PropertyAndField[] properties, ConstructorBuilder method)
{
ILGenerator iLGenerator = method.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
iLGenerator.Emit(OpCodes.Call, ObjectType.GetConstructor(Type.EmptyTypes));
int index = 0;
if (0 < properties.Length)
{
do
{
iLGenerator.Emit(OpCodes.Ldarg_0);
int arg = index + 1;
iLGenerator.Emit(OpCodes.Ldarg, arg);
iLGenerator.Emit(OpCodes.Stfld, properties[index].Field);
index = arg;
}
while (index < properties.Length);
}
iLGenerator.Emit(OpCodes.Ret);
}
示例7: SetUp
void SetUp (AssemblyBuilderAccess access)
{
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = ASSEMBLY_NAME;
assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
assemblyName, access,
Path.GetTempPath ());
module = assembly.DefineDynamicModule ("module1");
tb = module.DefineType ("Bar");
GenericTypeParameterBuilder [] typeParams = tb.DefineGenericParameters ("T");
cb = tb.DefineConstructor (MethodAttributes.Public,
CallingConventions.Standard,
new Type [] { typeof (string), typeof (int) });
ILGenerator ig = cb.GetILGenerator ();
ig.Emit (OpCodes.Ret);
typeBarOfInt32 = tb.MakeGenericType (typeof (int));
ci = TypeBuilder.GetConstructor (typeBarOfInt32, cb);
}
示例8: CopyMethodBody
void CopyMethodBody(MethodBase Base, ConstructorBuilder Builder)
{
if(HasBody(Base))
CopyMethodBody(Base, Builder.GetILGenerator());
}
示例9: Emitter
/// <summary>
/// Constructs an Emitter using the specified ConstructorBuilder.
/// </summary>
/// <param name="cntb">ConstructorBuilder</param>
public Emitter(ConstructorBuilder cntb)
{
if (cntb == null) {
throw new ArgumentNullException("cntb");
}
_il = cntb.GetILGenerator();
_temp = new Collection<LocalDescriptor>();
_code = new Collection<Instruction>();
}
示例10: Compile
public Type Compile()
{
TypeBuilder = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name,
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed);
var genericParameters = declaration.TypeParameters.Any() ? TypeBuilder.DefineGenericParameters(declaration.TypeParameters) : Type.EmptyTypes;
var containedType = new TypeConverter(runtimeContainer, genericParameters).Convert(declaration.Type);
field = TypeBuilder.DefineField(Constants.BoxedTypeValueFieldName, containedType, FieldAttributes.Public);
constructor = TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { containedType });
var cataCtorBody = constructor.GetILGenerator();
cataCtorBody.Emit(OpCodes.Ldarg_0);
cataCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
cataCtorBody.Emit(OpCodes.Ldarg_0);
cataCtorBody.Emit(OpCodes.Ldarg_1);
cataCtorBody.Emit(OpCodes.Stfld, field);
cataCtorBody.Emit(OpCodes.Ret);
MethodsType = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name + Constants.MethodsSuffix,
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Abstract);
CompileBoxFunction();
CompileUnboxFunction();
TypeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(SynonymAttribute).GetConstructors()[0],
new object[]
{
declaration.ConstructorFunctionName,
declaration.DestructorFunctionName,
UnboxFunction
}));
return TypeBuilder;
}
示例11: CompileUnboxFunction
private void CompileUnboxFunction()
{
UnboxFunction = MethodsType.DefineNestedType(Constants.UnboxFunctionClassName,
TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.Class,
null, Type.EmptyTypes);
var genericParameters = declaration.TypeParameters.Any() ? UnboxFunction.DefineGenericParameters(declaration.TypeParameters) : Type.EmptyTypes;
var containedType = new TypeConverter(runtimeContainer, genericParameters).Convert(declaration.Type);
UnboxFunction.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(TypeBuilder, containedType));
UnboxFunctionConstructor = UnboxFunction.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
var ctorBody = UnboxFunctionConstructor.GetILGenerator();
ctorBody.Emit(OpCodes.Ldarg_0);
ctorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
ctorBody.Emit(OpCodes.Ret);
var call = UnboxFunction.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual,
containedType, new Type[] { TypeBuilder });
var callBody = call.GetILGenerator();
callBody.Emit(OpCodes.Ldarg_1);
callBody.Emit(OpCodes.Ldfld, declaration.TypeParameters.Any()
? TypeBuilder.GetField(TypeBuilder.MakeGenericType(genericParameters), field)
: field);
callBody.Emit(OpCodes.Ret);
}
示例12: IlGenerator
// CodeDom生成
//public void CreateClass(Type defineType, string className, Dictionary<string, Type> dicProperty)
//{
// // 命名空间
// var nameSpace = new CodeNamespace(defineType.Namespace);
// // 定义一个新类
// var sortDeleteClass = new CodeTypeDeclaration(className)
// {
// IsClass = true,
// Attributes = MemberAttributes.Public
// };
// foreach (var keyValue in dicProperty)
// {
// var field = new CodeMemberField(keyValue.Value, "_" + keyValue.Key) { Attributes = MemberAttributes.Private };
// var property = new CodeMemberProperty()
// {
// Name = keyValue.Key,
// Type = new CodeTypeReference(keyValue.Value),
// Attributes = MemberAttributes.Private,
// };
// // 定义Get
// property.GetStatements.Add(new CodeMethodReturnStatement(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), field.Name)));
// // 定义Set
// property.SetStatements.Add(new CodeMethodReturnStatement(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), field.Name)));
// // 添加字段
// sortDeleteClass.Members.Add(field);
// // 添加属性
// sortDeleteClass.Members.Add(property);
// }
// nameSpace.Types.Add(sortDeleteClass);
// var cpr = CodeDomProvider.CreateProvider("C#");
// cpr.CompileAssemblyFromDom();
//}
/// <summary>
/// 定义方法体
/// </summary>
/// <param name="ctor">构造函数生成器</param>
private static void IlGenerator(ConstructorBuilder ctor)
{
var il = ctor.GetILGenerator();
il.Emit(OpCodes.Ldarg);
//il.Emit(OpCodes.Ldarg_0);
//il.Emit(OpCodes.Ldarg_1);
//il.Emit(OpCodes.Stfld, myGreetingField);
il.Emit(OpCodes.Ret);
}
示例13: CompileAnaClass
private void CompileAnaClass()
{
AnaClass = utilityClass.DefineNestedType(Constants.AnaClassName,
TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic,
null, TypeBuilder.EmptyTypes);
var anaClassGenericParameters = AnaClass.DefineGenericParameters(new[] { Constants.AnaClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray());
if (declaration.TypeParameters.Any())
{
AnaClass.AddInterfaceImplementation(TypeBuilder.MakeGenericType(anaClassGenericParameters.Skip(1).ToArray()));
}
else
{
AnaClass.AddInterfaceImplementation(TypeBuilder);
}
var anaClassGeneratorType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0],
FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaClassGenericParameters[0], anaClassGenericParameters.Skip(1).ToArray(), runtimeContainer));
var seedField = AnaClass.DefineField(Constants.AnaClassSeedFieldName, anaClassGenericParameters[0], FieldAttributes.Private);
var generatorField = AnaClass.DefineField(Constants.AnaClassGeneratorFieldName, anaClassGeneratorType, FieldAttributes.Private);
AnaClassConstructor = AnaClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { anaClassGenericParameters[0], anaClassGeneratorType });
var ctorBody = AnaClassConstructor.GetILGenerator();
ctorBody.Emit(OpCodes.Ldarg_0);
ctorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
ctorBody.Emit(OpCodes.Ldarg_0);
ctorBody.Emit(OpCodes.Ldarg_1);
ctorBody.Emit(OpCodes.Stfld, seedField);
ctorBody.Emit(OpCodes.Ldarg_0);
ctorBody.Emit(OpCodes.Ldarg_2);
ctorBody.Emit(OpCodes.Stfld, generatorField);
ctorBody.Emit(OpCodes.Ret);
var apply = AnaClass.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Virtual);
var resultType = apply.DefineGenericParameters(Constants.GFixFunctionClassGenericParameterName)[0];
var applyFunctionGenericParameters = new[] { resultType }.Concat(anaClassGenericParameters.Skip(1)).ToArray();
apply.SetParameters(GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters));
apply.SetReturnType(resultType);
var applyMethodGenericClass = TypeBuilder.GetMethod(
GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters),
GreatestFixedPointFunctionApplyMethod);
var applyMethodGenericMethod = applyMethodGenericClass.MakeGenericMethod(anaClassGenericParameters[0]);
var applyBody = apply.GetILGenerator();
applyBody.Emit(OpCodes.Ldarg_1);
applyBody.Emit(OpCodes.Ldarg_0);
applyBody.Emit(OpCodes.Ldfld, seedField);
applyBody.Emit(OpCodes.Ldarg_0);
applyBody.Emit(OpCodes.Ldfld, generatorField);
applyBody.Emit(OpCodes.Callvirt, applyMethodGenericMethod);
applyBody.Emit(OpCodes.Ret);
}
示例14: GetOrMakeInitializer
public CodeGen GetOrMakeInitializer()
{
if (initializer == null) {
initializer = myType.DefineTypeInitializer();
initGen = new CodeGen(this, initializer, initializer.GetILGenerator(), Type.EmptyTypes);
}
return initGen;
}
示例15: CompileOutClass
private void CompileOutClass()
{
OutClass = utilityClass.DefineNestedType(Constants.OutClassName,
TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic,
null, new[] { TypeBuilder });
var genericParameters = declaration.TypeParameters.Any() ? OutClass.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes;
var fLeastFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, TypeBuilder, genericParameters, runtimeContainer);
var predField = OutClass.DefineField(Constants.OutClassPredFieldName, fLeastFixedPoint, FieldAttributes.Private);
OutClassConstructor = OutClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { fLeastFixedPoint });
var outCtorBody = OutClassConstructor.GetILGenerator();
outCtorBody.Emit(OpCodes.Ldarg_0);
outCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
outCtorBody.Emit(OpCodes.Ldarg_0);
outCtorBody.Emit(OpCodes.Ldarg_1);
outCtorBody.Emit(OpCodes.Stfld, predField);
outCtorBody.Emit(OpCodes.Ret);
var cata = OutClass.DefineMethod(Constants.CataMethodName, MethodAttributes.Public | MethodAttributes.Virtual);
var genericParameter = cata.DefineGenericParameters(Constants.CataMethodGenericParameterName)[0];
var fGenericParameter = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, genericParameter, genericParameters, runtimeContainer);
cata.SetParameters(typeof(IFunction<,>).MakeGenericType(fGenericParameter, genericParameter));
cata.SetReturnType(genericParameter);
var cataBody = cata.GetILGenerator();
cataBody.Emit(OpCodes.Ldarg_1);
cataBody.Emit(OpCodes.Ldarg_1);
cataBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor(CataFunction.MakeGenericType(new[] { genericParameter }.Concat(genericParameters).ToArray()),
CataFunctionConstructor));
cataBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new Type[] { TypeBuilder, genericParameter }.Concat(genericParameters).ToArray()));
cataBody.Emit(OpCodes.Ldarg_0);
cataBody.Emit(OpCodes.Ldfld, predField);
cataBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod(
typeof(IFunction<,>).MakeGenericType(fLeastFixedPoint, fGenericParameter),
typeof(IFunction<,>).GetMethod(Constants.CallMethodName)));
cataBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod(
typeof(IFunction<,>).MakeGenericType(fGenericParameter, genericParameter),
typeof(IFunction<,>).GetMethod(Constants.CallMethodName)));
cataBody.Emit(OpCodes.Ret);
}