本文整理汇总了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"));
}
示例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);
}