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


C# IAssembly.Inject方法代码示例

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


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

示例1: StoreFactoryMap

        public void StoreFactoryMap(IAssembly assembly, FactoryMap factoryMap)
        {
            MethodInfo getTypeFromHandleReflect = typeof (Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) });
              MethodReference getTypeFromHandle = assembly.Import(getTypeFromHandleReflect);
              TypeReference systemType = assembly.Import(typeof(Type));
              TypeReference arrayOfTypes = assembly.Import(typeof(Type[]));
              TypeReference arrayOfArrayOfTypes = assembly.Import(typeof(Type[][]));

              // Now we create the actual type and stuff...
              TypeDefinition mapType = GetFactoryMapType(assembly);
              MethodDefinition getFactoriesMethod = new MethodDefinition(FactoryMapMethodName, MethodAttributes.Public, arrayOfArrayOfTypes);
              getFactoriesMethod.IsVirtual = true;
              mapType.Methods.Add(getFactoriesMethod);

              getFactoriesMethod.Body.Variables.Add(new VariableDefinition("map", 0, getFactoriesMethod, arrayOfArrayOfTypes));
              getFactoriesMethod.Body.Variables.Add(new VariableDefinition("row", 1, getFactoriesMethod, arrayOfTypes));
              getFactoriesMethod.Body.InitLocals = true;

              CilWorker worker = getFactoriesMethod.Body.CilWorker;
              List<Factory> factories = new List<Factory>(factoryMap.Factories);
              worker.Append(worker.Create(OpCodes.Nop));
              worker.Append(worker.Create(OpCodes.Ldc_I4, factories.Count));
              worker.Append(worker.Create(OpCodes.Newarr, arrayOfTypes));
              worker.Append(worker.Create(OpCodes.Stloc_0));
              worker.Append(worker.Create(OpCodes.Ldloc_0));

              int index = 0;
              foreach (Factory factory in factories)
              {
            worker.Append(worker.Create(OpCodes.Ldc_I4, index));

            worker.Append(worker.Create(OpCodes.Ldc_I4, 2));
            worker.Append(worker.Create(OpCodes.Newarr, systemType));
            worker.Append(worker.Create(OpCodes.Stloc_1));

            worker.Append(worker.Create(OpCodes.Ldloc_1));
            worker.Append(worker.Create(OpCodes.Ldc_I4_0));
            worker.Append(worker.Create(OpCodes.Ldtoken, factory.ObjectType));
            worker.Append(worker.Create(OpCodes.Call, getTypeFromHandle));
            worker.Append(worker.Create(OpCodes.Stelem_Ref));

            worker.Append(worker.Create(OpCodes.Ldloc_1));
            worker.Append(worker.Create(OpCodes.Ldc_I4_1));
            worker.Append(worker.Create(OpCodes.Ldtoken, factory.FactoryType));
            worker.Append(worker.Create(OpCodes.Call, getTypeFromHandle));
            worker.Append(worker.Create(OpCodes.Stelem_Ref));

            worker.Append(worker.Create(OpCodes.Ldloc_1));
            worker.Append(worker.Create(OpCodes.Stelem_Ref));
            index++;
            worker.Append(worker.Create(OpCodes.Ldloc_0));
              }

              worker.Append(worker.Create(OpCodes.Ret));
              assembly.Inject(mapType);
        }
开发者ID:benlovell,项目名称:machine,代码行数:56,代码来源:FactoryMapSerializer.cs


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