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


C# ModuleDefinition.ImportReference方法代码示例

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


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

示例1: FindReferences

    public static void FindReferences(IAssemblyResolver assemblyResolver, ModuleDefinition moduleDefinition)
    {
        var baseLib = assemblyResolver.Resolve("mscorlib");
        var baseLibTypes = baseLib.MainModule.Types;

        var winrt = baseLibTypes.All(type => type.Name != "Object");
        if (winrt)
        {
            baseLib = assemblyResolver.Resolve("System.Runtime");
            baseLibTypes = baseLib.MainModule.Types;
        }

        var argumentException = baseLibTypes.First(x => x.Name == "ArgumentException");
        ArgumentExceptionConstructor = moduleDefinition.ImportReference(argumentException.Methods.First(x =>
            x.IsConstructor &&
            x.Parameters.Count == 2 &&
            x.Parameters[0].ParameterType.Name == "String" &&
            x.Parameters[1].ParameterType.Name == "String"));

        var argumentNullException = baseLibTypes.First(x => x.Name == "ArgumentNullException");
        ArgumentNullExceptionConstructor = moduleDefinition.ImportReference(argumentNullException.Methods.First(x =>
            x.IsConstructor &&
            x.Parameters.Count == 1 &&
            x.Parameters[0].ParameterType.Name == "String"));
        ArgumentNullExceptionWithMessageConstructor = moduleDefinition.ImportReference(argumentNullException.Methods.First(x =>
            x.IsConstructor &&
            x.Parameters.Count == 2 &&
            x.Parameters[0].ParameterType.Name == "String" &&
            x.Parameters[1].ParameterType.Name == "String"));

        var invalidOperationException = baseLibTypes.First(x => x.Name == "InvalidOperationException");
        InvalidOperationExceptionConstructor = moduleDefinition.ImportReference(invalidOperationException.Methods.First(x =>
            x.IsConstructor &&
            x.Parameters.Count == 1 &&
            x.Parameters[0].ParameterType.Name == "String"));

        var debugLib = !winrt ? assemblyResolver.Resolve("System") : assemblyResolver.Resolve("System.Diagnostics.Debug");
        var debugLibTypes = debugLib.MainModule.Types;

        var debug = debugLibTypes.First(x => x.Name == "Debug");
        DebugAssertMethod = moduleDefinition.ImportReference(debug.Methods.First(x =>
            x.IsStatic &&
            x.Parameters.Count == 2 &&
            x.Parameters[0].ParameterType.Name == "Boolean" &&
            x.Parameters[1].ParameterType.Name == "String"));
    }
开发者ID:dpisanu,项目名称:NullGuard,代码行数:46,代码来源:ReferenceFinder.cs

示例2: AddConditionalBranchLong

    static void AddConditionalBranchLong(ModuleDefinition module, TypeDefinition type)
    {
        var method = new MethodDefinition("ConditionalBranchLong", Mono.Cecil.MethodAttributes.Public, module.TypeSystem.Boolean);
        var body = method.Body;
        body.InitLocals = true;
        var il = body.GetILProcessor();

        // This is the key: a call which will be replaced, targeted by a branch
        var call = il.Create(OpCodes.Callvirt, module.ImportReference(typeof(string).GetMethod("StartsWith", new[] {typeof(string)})));
        var branch = il.Create(OpCodes.Brtrue, call);

        il.Append(il.Create(OpCodes.Ldstr, "foo"));
        il.Append(il.Create(OpCodes.Ldstr, "F"));
        il.Append(il.Create(OpCodes.Dup));
        il.Append(branch);
        il.Append(il.Create(OpCodes.Pop));
        il.Append(il.Create(OpCodes.Ldstr, "G"));
        il.Append(call);
        il.Append(il.Create(OpCodes.Ret));

        type.Methods.Add(method);
    }
开发者ID:Fody,项目名称:Caseless,代码行数:22,代码来源:ModuleWeaverOperandTests.cs


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