Add a new varible to a method

223 views
Skip to first unread message

clark...@gmail.com

unread,
Mar 1, 2016, 6:20:19 AM3/1/16
to mono-cecil
I want to creat a new variable, for example, StreamWriter sw. I do like this:
            var stream_ref = new TypeReference("System.IO", "StreamWriter",MethodModule, null,true);
            var streamwriter = new VariableDefinition("writer", stream_ref);
            method.Body.Variables.Add(streamwriter);
and I get the NullReferenceException, I feel there lacks something, but I don't know where...I also wonder how to initialize the varible, like:
            StreamWriter sw = new StreamWriter("./test.txt", true);
and how to refer to it.
            Before, I try to create a varible like this:
            ins = ilProcessor.Create(OpCodes.Ldstr, @"./test.txt");
            ilProcessor.InsertBefore(instructions[0], ins);
            ins = ilProcessor.Create(OpCodes.Ldc_I4_1);
            ilProcessor.InsertBefore(method.Body.Instructions[1], ins);
            ins = ilProcessor.Create(OpCodes.Newobj, MethodModule.Import(typeof(System.IO.StreamWriter).GetConstructor( new Type[]{typeof(String), typeof(Boolean)})));
            ilProcessor.InsertBefore(instructions[2], ins);
            ins = ilProcessor.Create(OpCodes.Stloc_0);
            ilProcessor.InsertBefore(instructions[3], ins);
it doesn't work either.
I don't know why since the IL codes dumped from the one written in C# is like that, I know I miss something...
Another question is how to use the variable?

Jb Evain

unread,
Mar 1, 2016, 12:39:05 PM3/1/16
to mono-...@googlegroups.com
Hi,

The first thing you need to do is to actually create a representation
of the variable, with a proper type:

var streamWriterTypeRef = Module.Import(typeof(System.IO.StreamWriter));
var streamWriteCtorRef =
Module.Import(typeof(System.IO.StreamWriter).GetConstructor( new
Type[]{typeof(String), typeof(Boolean)})));

var streamWriterVar = new VariableDefinition("writer", stream_ref);

method.Variables.Add(streamWriterVar);

Then you can use it by either using

il.Create(OpCodes.Stloc, streamWriterVar);
il.Create(OpCodes.Ldloc, streamWriterVar);

Or, you can use the Ldloc_N version where N is the index of
streamWriterVar in the Variables collection.

Jb
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mono-cecil+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Jb Evain

unread,
Mar 2, 2016, 11:44:04 AM3/2/16
to mono-...@googlegroups.com
Cool !

I'm not exactly sure I understand what you mean.

Import (ImportReference in master), creates a reference scoped to your
module from a metadata element defined in another module. This is
conceptually different from a C# using.

Jb


On Wed, Mar 2, 2016 at 7:23 AM, <clark...@gmail.com> wrote:
> Thanks, it works! I wonder if the Import works like using.... in the C#
> code, but it works in minor scale, such as the type reference but not
> assembly reference?
>
> 在 2016年3月1日星期二 UTC+8下午7:20:19,clark...@gmail.com写道:

clark...@gmail.com

unread,
Mar 3, 2016, 10:43:25 AM3/3/16
to mono-cecil
Thanks for your explantion, I am a green hand, so maybe I need more time the understand it. 
I clearly know Mono.cecil can do everything to a C# program, because it can read the original program and create new instructions to it. 
There is another question is: can it combines two programs into one or introduces some part of a program to another one?For example:
There is a program like:
Assembly A contains:
class program 1
{
     public main(){}
}
Assembly B contains:
class program 2
{
     variable a;
     public fun(){ a...}
}
I wonder how to introduce the fun() in program 2 to program 1. if this can be done, I can surely use 
Mono.cecil to let main() call the fun(). Now my initial idea is to create the variable and fun() in program 1, just 
the  same to program 2
but it feels a bit strange, is there a way to introduce them to program 1?


在 2016年3月3日星期四 UTC+8上午12:44:04,Jb Evain写道:

Jb Evain

unread,
Mar 3, 2016, 11:47:56 AM3/3/16
to mono-...@googlegroups.com
It's possible, but it's not trivial.

IL-Repack does this: https://github.com/gluck/il-repack

Jb

clark...@gmail.com

unread,
Mar 3, 2016, 9:54:25 PM3/3/16
to mono-cecil
Thank you very much! :) I will read it carefully.

在 2016年3月4日星期五 UTC+8上午12:47:56,Jb Evain写道:
Reply all
Reply to author
Forward
0 new messages