本文整理汇总了C#中CodeGenContext.GetWriterMethodInfo方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenContext.GetWriterMethodInfo方法的具体用法?C# CodeGenContext.GetWriterMethodInfo怎么用?C# CodeGenContext.GetWriterMethodInfo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenContext
的用法示例。
在下文中一共展示了CodeGenContext.GetWriterMethodInfo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateWriterMethod
public void GenerateWriterMethod(Type obtype, CodeGenContext ctx, ILGenerator il)
{
var getTypeIDMethodInfo = typeof(Serializer).GetMethod("GetTypeID", BindingFlags.NonPublic | BindingFlags.Instance, null,
new Type[] { typeof(object) }, null);
var map = ctx.TypeMap;
// arg0: Serializer, arg1: Stream, arg2: object
var idLocal = il.DeclareLocal(typeof(ushort));
// get TypeID from object's Type
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Call, getTypeIDMethodInfo);
il.Emit(OpCodes.Stloc_S, idLocal);
// write typeID
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldloc_S, idLocal);
il.Emit(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(ushort)));
// +1 for 0 (null)
var jumpTable = new Label[map.Count + 1];
jumpTable[0] = il.DefineLabel();
foreach (var kvp in map)
jumpTable[kvp.Value.TypeID] = il.DefineLabel();
il.Emit(OpCodes.Ldloc_S, idLocal);
il.Emit(OpCodes.Switch, jumpTable);
il.Emit(OpCodes.Newobj, Helpers.ExceptionCtorInfo);
il.Emit(OpCodes.Throw);
/* null case */
il.MarkLabel(jumpTable[0]);
il.Emit(OpCodes.Ret);
/* cases for types */
foreach (var kvp in map)
{
var type = kvp.Key;
var data = kvp.Value;
il.MarkLabel(jumpTable[data.TypeID]);
if (data.NeedsInstanceParameter)
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, type);
il.Emit(OpCodes.Tailcall);
il.Emit(OpCodes.Call, data.WriterMethodInfo);
il.Emit(OpCodes.Ret);
}
}
示例2: GenerateWriterMethod
public void GenerateWriterMethod(Type type, CodeGenContext ctx, ILGenerator il)
{
var elemType = type.GetElementType();
var notNullLabel = il.DefineLabel();
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Brtrue_S, notNullLabel);
// if value == null, write 0
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Tailcall);
il.Emit(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(uint)));
il.Emit(OpCodes.Ret);
il.MarkLabel(notNullLabel);
// write array len + 1
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldlen);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(uint)));
// declare i
var idxLocal = il.DeclareLocal(typeof(int));
// i = 0
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Stloc_S, idxLocal);
var loopBodyLabel = il.DefineLabel();
var loopCheckLabel = il.DefineLabel();
il.Emit(OpCodes.Br_S, loopCheckLabel);
// loop body
il.MarkLabel(loopBodyLabel);
var data = ctx.GetTypeDataForCall(elemType);
if (data.NeedsInstanceParameter)
il.Emit(OpCodes.Ldarg_0);
// write element at index i
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldloc_S, idxLocal);
il.Emit(OpCodes.Ldelem, elemType);
il.Emit(OpCodes.Call, data.WriterMethodInfo);
// i = i + 1
il.Emit(OpCodes.Ldloc_S, idxLocal);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc_S, idxLocal);
il.MarkLabel(loopCheckLabel);
// loop condition
il.Emit(OpCodes.Ldloc_S, idxLocal);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldlen);
il.Emit(OpCodes.Conv_I4);
il.Emit(OpCodes.Clt);
il.Emit(OpCodes.Brtrue_S, loopBodyLabel);
il.Emit(OpCodes.Ret);
}
示例3: GenerateWriterMethod
public void GenerateWriterMethod(Type type, CodeGenContext ctx, ILGenerator il)
{
var elemType = type.GetElementType();
var notNullLabel = il.DefineLabel();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Brtrue_S, notNullLabel);
// if value == null, write uint.MaxValue
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldc_I4, uint.MaxValue);
il.EmitCall(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(uint)), null);
il.Emit(OpCodes.Ret);
il.MarkLabel(notNullLabel);
// write array len
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldlen);;
il.EmitCall(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(uint)), null);
// declare i
var idxLocal = il.DeclareLocal(typeof(int));
// i = 0
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Stloc_S, idxLocal);
var loopBodyLabel = il.DefineLabel();
var loopCheckLabel = il.DefineLabel();
il.Emit(OpCodes.Br_S, loopCheckLabel);
// loop body
il.MarkLabel(loopBodyLabel);
// write element at index i
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldloc_S, idxLocal);
il.Emit(OpCodes.Ldelem, elemType);
Helpers.GenSerializerCall(ctx, il, elemType);
// i = i + 1
il.Emit(OpCodes.Ldloc_S, idxLocal);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc_S, idxLocal);
il.MarkLabel(loopCheckLabel);
// loop condition
il.Emit(OpCodes.Ldloc_S, idxLocal);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldlen);
il.Emit(OpCodes.Conv_I4);
il.Emit(OpCodes.Clt);
il.Emit(OpCodes.Brtrue_S, loopBodyLabel);
il.Emit(OpCodes.Ret);
}