本文整理汇总了C#中TypeBuilder.DefineTypeInitializer方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineTypeInitializer方法的具体用法?C# TypeBuilder.DefineTypeInitializer怎么用?C# TypeBuilder.DefineTypeInitializer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineTypeInitializer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteConstructors
private void WriteConstructors(TypeBuilder type, ref int index, SerializerPair[] methodPairs, ref ILGenerator il, int knownTypesCategory, FieldBuilder knownTypes, Type knownTypesLookupType, Compiler.CompilerContext ctx)
{
type.DefineDefaultConstructor(MethodAttributes.Public);
il = type.DefineTypeInitializer().GetILGenerator();
switch (knownTypesCategory)
{
case KnownTypes_Array:
{
Compiler.CompilerContext.LoadValue(il, types.Count);
il.Emit(OpCodes.Newarr, ctx.MapType(typeof(System.Type)));
index = 0;
foreach (SerializerPair pair in methodPairs)
{
il.Emit(OpCodes.Dup);
Compiler.CompilerContext.LoadValue(il, index);
il.Emit(OpCodes.Ldtoken, pair.Type.Type);
il.EmitCall(OpCodes.Call, ctx.MapType(typeof(System.Type)).GetMethod("GetTypeFromHandle"), null);
il.Emit(OpCodes.Stelem_Ref);
index++;
}
il.Emit(OpCodes.Stsfld, knownTypes);
il.Emit(OpCodes.Ret);
}
break;
case KnownTypes_Dictionary:
{
Compiler.CompilerContext.LoadValue(il, types.Count);
//LocalBuilder loc = il.DeclareLocal(knownTypesLookupType);
il.Emit(OpCodes.Newobj, knownTypesLookupType.GetConstructor(new Type[] { MapType(typeof(int)) }));
il.Emit(OpCodes.Stsfld, knownTypes);
int typeIndex = 0;
foreach (SerializerPair pair in methodPairs)
{
il.Emit(OpCodes.Ldsfld, knownTypes);
il.Emit(OpCodes.Ldtoken, pair.Type.Type);
il.EmitCall(OpCodes.Call, ctx.MapType(typeof(System.Type)).GetMethod("GetTypeFromHandle"), null);
int keyIndex = typeIndex++, lastKey = pair.BaseKey;
if (lastKey != pair.MetaKey) // not a base-type; need to give the index of the base-type
{
keyIndex = -1; // assume epic fail
for (int j = 0; j < methodPairs.Length; j++)
{
if (methodPairs[j].BaseKey == lastKey && methodPairs[j].MetaKey == lastKey)
{
keyIndex = j;
break;
}
}
}
Compiler.CompilerContext.LoadValue(il, keyIndex);
il.EmitCall(OpCodes.Callvirt, knownTypesLookupType.GetMethod("Add", new Type[] { MapType(typeof(System.Type)), MapType(typeof(int)) }), null);
}
il.Emit(OpCodes.Ret);
}
break;
case KnownTypes_Hashtable:
{
Compiler.CompilerContext.LoadValue(il, types.Count);
il.Emit(OpCodes.Newobj, knownTypesLookupType.GetConstructor(new Type[] { MapType(typeof(int)) }));
il.Emit(OpCodes.Stsfld, knownTypes);
int typeIndex = 0;
foreach (SerializerPair pair in methodPairs)
{
il.Emit(OpCodes.Ldsfld, knownTypes);
il.Emit(OpCodes.Ldtoken, pair.Type.Type);
il.EmitCall(OpCodes.Call, ctx.MapType(typeof(System.Type)).GetMethod("GetTypeFromHandle"), null);
int keyIndex = typeIndex++, lastKey = pair.BaseKey;
if (lastKey != pair.MetaKey) // not a base-type; need to give the index of the base-type
{
keyIndex = -1; // assume epic fail
for (int j = 0; j < methodPairs.Length; j++)
{
if (methodPairs[j].BaseKey == lastKey && methodPairs[j].MetaKey == lastKey)
{
keyIndex = j;
break;
}
}
}
Compiler.CompilerContext.LoadValue(il, keyIndex);
il.Emit(OpCodes.Box, MapType(typeof(int)));
il.EmitCall(OpCodes.Callvirt, knownTypesLookupType.GetMethod("Add", new Type[] { MapType(typeof(object)), MapType(typeof(object)) }), null);
}
il.Emit(OpCodes.Ret);
}
break;
default:
throw new InvalidOperationException();
}
}