当前位置: 首页>>代码示例>>C#>>正文


C# ILGenerator.EmitSt方法代码示例

本文整理汇总了C#中System.Reflection.Emit.ILGenerator.EmitSt方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.EmitSt方法的具体用法?C# ILGenerator.EmitSt怎么用?C# ILGenerator.EmitSt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.ILGenerator的用法示例。


在下文中一共展示了ILGenerator.EmitSt方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EmitPackArrayCode

		static void EmitPackArrayCode (MethodInfo mi, ILGenerator il, Type t, Variable var_writer, Variable var_obj, Variable var_loop, Func<Type, MethodInfo> lookupPackMethod)
		{
			Type et = t.GetElementType ();
			il.EmitLd (var_writer, var_obj);
			il.Emit (OpCodes.Ldlen);
			il.Emit (OpCodes.Call, typeof(MsgPackWriter).GetMethod("WriteArrayHeader", new Type[]{ typeof(int) }));

			Label beginLabel = il.DefineLabel ();
			Label exprLabel = il.DefineLabel ();

			// for-loop: init loop counter
			il.EmitLdc (0);
			il.EmitSt (var_loop);

			// jump
			il.Emit (OpCodes.Br_S, exprLabel);

			// mark begin-label
			il.MarkLabel (beginLabel);

			// write element
			EmitPackMemberValueCode (et, il, var_writer, var_obj, null, var_loop, t, mi, lookupPackMethod);

			// increment loop-counter
			il.EmitLd (var_loop);
			il.Emit (OpCodes.Ldc_I4_1);
			il.Emit (OpCodes.Add);
			il.EmitSt (var_loop);

			// mark expression label
			il.MarkLabel (exprLabel);

			// expression
			il.EmitLd (var_loop, var_obj);
			il.Emit (OpCodes.Ldlen);
			il.Emit (OpCodes.Blt_S, beginLabel);
		}
开发者ID:dhorio,项目名称:sample,代码行数:37,代码来源:PackILGenerator.cs

示例2: EmitUnpackMapCode

		static void EmitUnpackMapCode (Type type, MethodInfo mi, ILGenerator il,
			Func<Type,MemberInfo[]> targetMemberSelector,
			Func<MemberInfo,string> memberNameFormatter,
			Func<Type, MethodInfo> lookupUnpackMethod,
			Func<Type, IDictionary<string,int>> lookupMemberMapping,
			MethodInfo lookupMemberMappingMethod)
		{
			MethodInfo failedMethod = typeof (PackILGenerator).GetMethod ("UnpackFailed", BindingFlags.Static | BindingFlags.NonPublic);
			MemberInfo[] members = targetMemberSelector (type);
			IDictionary<string, int> member_mapping = lookupMemberMapping (type);
			for (int i = 0; i < members.Length; i ++)
				member_mapping.Add (memberNameFormatter (members[i]), i);

			Variable msgpackReader = Variable.CreateArg (0);
			Variable obj = Variable.CreateLocal (il.DeclareLocal (type));
			Variable num_of_fields = Variable.CreateLocal (il.DeclareLocal (typeof (int)));
			Variable loop_idx = Variable.CreateLocal (il.DeclareLocal (typeof (int)));
			Variable mapping = Variable.CreateLocal (il.DeclareLocal (typeof (IDictionary<string, int>)));
			Variable switch_idx = Variable.CreateLocal (il.DeclareLocal (typeof (int)));
			Variable var_type = Variable.CreateLocal (il.DeclareLocal (typeof (Type)));

			// if (!MsgPackReader.Read()) UnpackFailed ();
			// if (MsgPackReader.Type == TypePrefixes.Nil) return null;
			// if (!MsgPackReader.IsMap ()) UnpackFailed ();
			EmitUnpackReadAndTypeCheckCode (il, msgpackReader, typeof (MsgPackReader).GetMethod ("IsMap"), failedMethod, true);
			
			// type = typeof (T)
			il.Emit (OpCodes.Ldtoken, type);
			il.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
			il.EmitSt (var_type);

			// mapping = LookupMemberMapping (typeof (T))
			il.EmitLd (var_type);
			il.Emit (OpCodes.Call, lookupMemberMappingMethod);
			il.EmitSt (mapping);

			// object o = FormatterServices.GetUninitializedObject (Type);
			il.EmitLd (var_type);
			il.Emit (OpCodes.Call, typeof (FormatterServices).GetMethod ("GetUninitializedObject"));
			il.Emit (OpCodes.Castclass, type);
			il.EmitSt (obj);

			// num_of_fields = (int)reader.Length
			il.EmitLd (msgpackReader);
			il.Emit (OpCodes.Call, typeof (MsgPackReader).GetProperty ("Length").GetGetMethod ());
			il.EmitSt (num_of_fields);

			// Loop labels
			Label lblLoopStart = il.DefineLabel ();
			Label lblLoopExpr = il.DefineLabel ();
			
			// i = 0;
			il.EmitLdc (0);
			il.EmitSt (loop_idx);
			il.Emit (OpCodes.Br, lblLoopExpr);
			il.MarkLabel (lblLoopStart);

			/* process */
			// if (!MsgPackReader.Read() || !MsgPackReader.IsRaw()) UnpackFailed();
			EmitUnpackReadAndTypeCheckCode (il, msgpackReader, typeof (MsgPackReader).GetMethod ("IsRaw"), failedMethod, false);

			// MsgPackReader.ReadRawString ()
			// if (!Dictionary.TryGetValue (,)) UnpackFailed();
			Label lbl3 = il.DefineLabel ();
			il.EmitLd (mapping);
			il.EmitLd (msgpackReader);
			il.Emit (OpCodes.Call, typeof (MsgPackReader).GetMethod ("ReadRawString", new Type[0]));
			il.Emit (OpCodes.Ldloca_S, (byte)switch_idx.Index);
			il.Emit (OpCodes.Callvirt, typeof (IDictionary<string,int>).GetMethod ("TryGetValue"));
			il.Emit (OpCodes.Brtrue, lbl3);
			il.Emit (OpCodes.Call, failedMethod);
			il.MarkLabel (lbl3);

			// switch
			Label[] switchCases = new Label[members.Length];
			for (int i = 0; i < switchCases.Length; i ++)
				switchCases[i] = il.DefineLabel ();
			Label switchCaseEndLabel = il.DefineLabel ();
			il.EmitLd (switch_idx);
			il.Emit (OpCodes.Switch, switchCases);
			il.Emit (OpCodes.Call, failedMethod);

			for (int i = 0; i < switchCases.Length; i ++) {
				il.MarkLabel (switchCases[i]);
				MemberInfo minfo = members[i];
				Type mt = minfo.GetMemberType ();
				MethodInfo unpack_method = lookupUnpackMethod (mt);
				il.EmitLd (obj);
				il.EmitLd (msgpackReader);
				il.Emit (OpCodes.Call, unpack_method);
				il.EmitStMember (minfo);
				il.Emit (OpCodes.Br, switchCaseEndLabel);
			}
			il.MarkLabel (switchCaseEndLabel);

			// i ++
			il.EmitLd (loop_idx);
			il.EmitLdc (1);
			il.Emit (OpCodes.Add);
			il.EmitSt (loop_idx);
//.........这里部分代码省略.........
开发者ID:dhorio,项目名称:sample,代码行数:101,代码来源:PackILGenerator.cs

示例3: EmitUnpackArrayCode

		static void EmitUnpackArrayCode (Type arrayType, MethodInfo mi, ILGenerator il,
			Func<Type,MemberInfo[]> targetMemberSelector,
			Func<MemberInfo,string> memberNameFormatter,
			Func<Type, MethodInfo> lookupUnpackMethod)
		{
			Type elementType = arrayType.GetElementType ();
			MethodInfo failedMethod = typeof (PackILGenerator).GetMethod ("UnpackFailed", BindingFlags.Static | BindingFlags.NonPublic);

			Variable msgpackReader = Variable.CreateArg (0);
			Variable obj = Variable.CreateLocal (il.DeclareLocal (arrayType));
			Variable num_of_elements = Variable.CreateLocal (il.DeclareLocal (typeof (int)));
			Variable loop_idx = Variable.CreateLocal (il.DeclareLocal (typeof (int)));
			Variable type = Variable.CreateLocal (il.DeclareLocal (typeof (Type)));

			// if (!MsgPackReader.Read() || !MsgPackReader.IsArray ()) UnpackFailed ();
			EmitUnpackReadAndTypeCheckCode (il, msgpackReader, typeof (MsgPackReader).GetMethod ("IsArray"), failedMethod, true);

			// type = typeof (T)
			il.Emit (OpCodes.Ldtoken, elementType);
			il.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
			il.EmitSt (type);

			// num_of_elements = (int)reader.Length
			il.EmitLd (msgpackReader);
			il.Emit (OpCodes.Call, typeof (MsgPackReader).GetProperty ("Length").GetGetMethod ());
			il.EmitSt (num_of_elements);

			// object o = Array.CreateInstance (Type, Length);
			il.EmitLd (type);
			il.EmitLd (num_of_elements);
			il.Emit (OpCodes.Call, typeof (Array).GetMethod ("CreateInstance", new Type[] {typeof (Type), typeof (int)}));
			il.Emit (OpCodes.Castclass, arrayType);
			il.EmitSt (obj);

			// Unpack element method
			MethodInfo unpack_method = lookupUnpackMethod (elementType);

			// Loop labels
			Label lblLoopStart = il.DefineLabel ();
			Label lblLoopExpr = il.DefineLabel ();
			
			// i = 0;
			il.EmitLdc (0);
			il.EmitSt (loop_idx);
			il.Emit (OpCodes.Br, lblLoopExpr);
			il.MarkLabel (lblLoopStart);

			/* process */
			il.EmitLd (obj, loop_idx);
			il.EmitLd (msgpackReader);
			il.Emit (OpCodes.Call, unpack_method);
			il.Emit (OpCodes.Stelem, elementType);

			// i ++
			il.EmitLd (loop_idx);
			il.EmitLdc (1);
			il.Emit (OpCodes.Add);
			il.EmitSt (loop_idx);

			// i < num_of_fields;
			il.MarkLabel (lblLoopExpr);
			il.EmitLd (loop_idx);
			il.EmitLd (num_of_elements);
			il.Emit (OpCodes.Blt, lblLoopStart);
			
			// return
			il.EmitLd (obj);
			il.Emit (OpCodes.Ret);
		}
开发者ID:dhorio,项目名称:sample,代码行数:69,代码来源:PackILGenerator.cs

示例4: EmitUnpackIL

        public static void EmitUnpackIL(Type mapType, MethodInfo mi, ILGenerator il,
    Func<Type, MemberInfo[]> targetMemberSelector,
    Func<MemberInfo, string> memberNameFormatter,
    Func<Type, MethodInfo> lookupUnpackMethod)
        {
            // References
            var failedMethod = typeof(PackILGenerator).GetMethod("UnpackFailed", BindingFlags.Static | BindingFlags.NonPublic);
            // Variables
            var obj = Variable.CreateLocal(il.DeclareLocal(mapType));
            var msgpackReader = Variable.CreateArg(0);
            var var_type = Variable.CreateLocal(il.DeclareLocal(typeof(Type)));
            var num_of_fields = Variable.CreateLocal(il.DeclareLocal(typeof(int)));

            PackILGenerator.EmitUnpackReadAndTypeCheckCode(il, msgpackReader, typeof(MsgPackReader).GetMethod("IsMap"), failedMethod, true);

            // num_of_fields = (int)reader.Length
            il.EmitLd(msgpackReader);
            il.Emit(OpCodes.Call, typeof(MsgPackReader).GetProperty("Length").GetGetMethod());
            il.EmitSt(num_of_fields);

            // mapType
            il.EmitLd(num_of_fields);
            il.Emit(OpCodes.Newobj, mapType.GetConstructor(new Type[] { typeof(int) }));
            il.EmitSt(obj);

            // Problems with this: It only reads empty dictionaries.

            // return
            il.EmitLd(obj);
            il.Emit(OpCodes.Ret);
        }
开发者ID:e-travel,项目名称:msgpack,代码行数:31,代码来源:DictionaryILGenerator.cs


注:本文中的System.Reflection.Emit.ILGenerator.EmitSt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。