mono.cecil

Inject Method with Mono.Cecil

烂漫一生 提交于 2019-12-13 06:24:02
问题 How can I inject a custom method into a .net assembly using mono.cecil, and then call it in the entrypoint? I like to do this to implement security methods after the binary is built. 回答1: To inject method you need to get the type you want to add it the method and then add a MethoDefinition . var mainModule = ModuleDefinition.ReadModule(assemblyPath); var type = module.Types.Single(t => t.Name == "TypeYouWant"); var newMethodDef= new MethodDefinition("Name", MethodAttributes.Public, mainModule

Mono.Cecil unresolved tokens when renaming a generic type's field

跟風遠走 提交于 2019-12-12 22:08:58
问题 When I read an assembly and rename a field that's contained inside a generic type, and then write the assembly, all references to this field inside the methods become unresolved tokens. I know that the same also happens to method references if I rename them. // This is a generic class. TypeDefinition type = GetSomeGenericType(); // This field is referenced from a method in this type. type.Fields[0].Name = "_anotherName"; assemblyDefinition.Write(...); The resulting assembly won't work. Is

Custom IL Rewriting plugin for msbuild

二次信任 提交于 2019-12-12 14:10:44
问题 I want to create a custom msbuild task that applies IL rwriting to my output assembly. At the moment i am already using PostSharp and now try to extend the rewriting capabilities. For some special cases i use Mono.Cecil to rewrite some proxy types into the assembly. This works fine right now. But now i want to intercept the build process between the actual build and the PostSharp transformation in order to generate aspects on my proxy types that get implemented by PostSharp in the next step.

How can I remove all DebuggerHiddenAttribute

蓝咒 提交于 2019-12-11 20:11:32
问题 How can I remove all DebuggerHiddenAttribute from an assembly only if the compiler generated this attribute? I'm trying this code, but it doesn't work. ModuleDefinition module = ...; MethodDefinition targetMethod = ...; MethodReference attributeConstructor = module.Import( typeof(DebuggerHiddenAttribute).GetConstructor(Type.EmptyTypes)); targetMethod.CustomAttributes.Remove(new CustomAttribute(attributeConstructor)); module.Write(...); Thanks in advance. 回答1: This is not really possible. The

Cannot set the base class of a type with Mono Cecil

戏子无情 提交于 2019-12-11 20:01:51
问题 I am trying to use Mono Cecil to manipulate a DLL. What I want to do is to set the base type of some classes to System.MarshalByRefObject from mscorlib.dll . I am using the following code. var assembly = AssemblyDefinition.ReadAssembly(inputtarget); var types = assembly.MainModule.Types; var myTypesToChange = types.Where(...).Select(t => t); var baseType = AssemblyDefinition.ReadAssembly(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll").MainModule.Types.Where(t => t.Name

Preprocessing C# - Detecting Methods

心不动则不痛 提交于 2019-12-11 01:55:54
问题 I require the ability to preprocess a number of C# files as a prebuild step for a project, detect the start of methods, and insert generated code at the start of the method, before any existing code. I am, however, having a problem detecting the opening of a method. I initially tried a regular expression to match, but ended up with far too many false positives. I would use reflection, but the MethodInfo class does not reference the point in the original source. EDIT : What I am really trying

Mono.Cecil: call base class' method from other assembly

泄露秘密 提交于 2019-12-10 21:49:28
问题 How can I get a MethodReference to a base class' method by name? I've tried type.BaseType.Resolve().Methods; and if I add the dll containing the base class to the assemblyresolver it returns the methods. But if I add a call using MSILWorker.Create(OpCodes.Call, baseMethod); (where baseMethod was found by foreaching Methods from the resolved TypeDefinition) the resulting IL is unreadable, even Reflector freezes and quits. Now some IL: if calling private method on type: call instance void

Mono Cecil error generating PDB

天涯浪子 提交于 2019-12-10 16:48:05
问题 The code below allows me to change methods body and save back the assembly. // Recreate PDB var assemblyResolver = new DefaultAssemblyResolver(); var assemblyLocation = Path.GetDirectoryName(pathBin); assemblyResolver.AddSearchDirectory(assemblyLocation); /*if (!string.IsNullOrEmpty(HintPath)) { assemblyResolver.AddSearchDirectory(HintPath); }*/ var silverlightAssemblyPath = Environment.ExpandEnvironmentVariables(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\"); assemblyResolver

Member is declared in another module and needs to be imported

江枫思渺然 提交于 2019-12-10 13:27:38
问题 I use Mono.Cecil to create a new custom attribute type and then add it to an existing type. To demonstrate it, I have a pre-existing DLL called "Sample" with type that is called "SampleType". I want to use Mono.Cecil to weave in a new type in "Sample" called "NewAttribute" and then add this attribute to "SampleType". The code looks like this: (not exactly but its good enough for example) static void AddCustomeAttribute() { var module = ModuleDefinition.ReadModule(AssemblyName); var attrType =