Re: [mono-cecil] How to call a reference in a different version of a .Net assembly?

49 views
Skip to first unread message

Jb Evain

unread,
Sep 20, 2012, 12:54:56 PM9/20/12
to mono-...@googlegroups.com
Hi,

It's quite simple, you have to use the Cecil type system instead of
System.Reflection which is bound to the executing version.

Open the assembly containing Encoding.UTF8 you need with Cecil, and
import its reference in your module.

Best,

Jb

On Thu, Sep 20, 2012 at 6:19 PM, earlz <ea...@lastyearswishes.com> wrote:
> Hi there, I've been trying to create a small project that basically will
> intercept a call to a certain method in a WinRT application and replace the
> call with a generated static method.
>
> Everything has been working great until now. I inject the code with a
> regular desktop application. The problem is that I use
>
> il.Emit(OpCodes.Call,
> Module.Import(typeof(System.Text.Encoding).GetMethod("get_UTF8")));
>
> This generates the il
>
> call class [mscorlib]System.Text.Encoding
> [mscorlib]System.Text.Encoding::get_UTF8()
>
> But, I appear to need it to generate
>
> call class [System.Text.Encoding]System.Text.Encoding
> [System.Text.Encoding]System.Text.Encoding::get_UTF8()
>
>
>
> How do I go about adding a type reference to a built in assembly that isn't
> in my injecting application?
>
>
>
>
> --
> --
> mono-cecil

earlz

unread,
Sep 20, 2012, 2:45:20 PM9/20/12
to mono-...@googlegroups.com
I ended up finding a hacky workaround (gather type references from the assembly). This project is just a quick prototype so it doesn't matter.

For that solution though, are you sure that would work? I believe I did that sorta and ended up getting (swallowed) exceptions at runtime for "The file System.Text.Encoding.dll could not be found" presumably due to the WinRT sandbox. I noticed also the issue in your issue tracker about better WinRT support in general. I assume all of this will get a lot easier when that gets worked on?

Jb Evain

unread,
Sep 20, 2012, 3:12:05 PM9/20/12
to mono-...@googlegroups.com
Yes I'm sure that would work. That's actually the recommended way to do it.

The only thing that could require some custom handling is an assembly
resolver for WinRT assemblies.

Jb
> --
> --
> mono-cecil
Reply all
Reply to author
Forward
0 new messages