On Sep 7, 2:03 pm, Simon Cropp <[email protected]> wrote:
> Vijay
> perhaps upload a small sample project illustrating your issue?
>
I realised my stupidity just after clicking on send button :)
Here is the code that I extracted out of my application code (which I
cannot post here).
In my excitement to get reply, I might have forgotten something,
apologies in advance for that.
private void AddProfilingMethodToAssembly()
{
var md = new MethodDefinition("ProfilerMEthod", // was
ProfilerMethodInSourceAssembly in source assembly
MethodAttributes.HideBySig |
MethodAttributes.Static | MethodAttributes.CompilerControlled,
assemblyDefinition.MainModule.Import(typeof(string)));
//add it to <Module> class
typeDefinition.Methods.Add(md);
//Generate the parameters
md.Parameters.Add(new ParameterDefinition("v",
ParameterAttributes.None,
assemblyDefinition.MainModule.Import(typeof(string))));
//create a method body
md.Body = new MethodBody(md);
var body = md.Body;
foreach (var mi in typeof(string).GetMethods())
{
var mr =
body.Method.Module.Assembly.MainModule.Import(mi);
body.Method.Module.Assembly.MainModule.Import(mr);
}
var methodBody = ProfilerMethodDefiniton();
body.InitLocals = true;
foreach (var local in methodBody .Body.Variables)
{
body.Variables.Add(local);
}
body.Instructions.Clear();
var worker = body.GetILProcessor();
foreach (var instruction in methodBody.Body.Instructions)
{
worker.Append(instruction);
}
}
public MethodDefinition ProfilerMethodDefiniton()
{
//I am trying to extract method from currently running
assembly
var asm =
AssemblyDefinition.ReadAssembly(System.Reflection.Assembly.GetExecutingAssembly().Location);
foreach (var type in asm.MainModule.GetAllTypes())
{
foreach (var methodDefinition in type.Methods)
{
if
(methodDefinition.Name.Equals("ProfilerMethodInSourceAssembly"))
return methodDefinition;
}
}
return null;
}
// this is the method that is present in currently executing
assembly, but gets added to target assembly
public static void ProfilerMethodInSourceAssembly(string
value)
{
// do something with the string here
}
Thanks
Vijay
--
--
mono-cecil