all groups > dotnet clr > march 2007 >
You're in the

dotnet clr

group:

Create IL Stream from source code


Create IL Stream from source code John Rivers
3/6/2007 4:13:41 PM
dotnet clr:
Hi

Does anybody know a way to create an IL stream from source code?

I am trying to create a DynamicMethod object ideally from CSharp
source code

using GetDynamicILInfo it is possible to pass raw IL to define the
method body

maybe there is a way to extract IL from a .netmodule file ?

John
Re: Create IL Stream from source code Barry Kelly
3/7/2007 6:25:17 PM
[quoted text, click to view]

MethodBase.GetMethodBody() returns MethodBody, which has various methods
and properties, such as 'GetILAsByteArray()' and 'LocalVariables' etc.

-- Barry

--
Re: Create IL Stream from source code John Rivers
3/14/2007 4:29:55 PM

Thanks for your reply

Unfortunately I can only call
MethodBase.GetMethodBody() and GetILAsByteArray()
on a loaded assembly
which is what I am trying to avoid
as you can not unload dot net DLL





[quoted text, click to view]

Re: Create IL Stream from source code John Rivers
3/15/2007 12:43:33 AM


That is a very good idea *-)

At last my scripting engine will work ...

- pass Source code to method in DLL in new AppDomain
- method compiles and returns IL bytecode as byte[]
- Unload AppDomain
- create DynamicMethod with returned bytecode

Thanks Barry


[quoted text, click to view]

Re: Create IL Stream from source code Barry Kelly
3/15/2007 1:10:08 AM
[quoted text, click to view]

You could do it in another AppDomain, and marshal the array across, no?

That way, you can unload the other AppDomain and the assemblies in it.

-- Barry

--
Re: Create IL Stream from source code John Rivers
3/15/2007 10:28:50 AM
Hi Barry

I have written the scripting engine as you suggested.

And that approach is working fine

Now I am getting stuck problems such as:

"Common Language Runtime detected an invalid program."
"Bad method token."

I have success with methods such as:

public void Test() {
}//method

and this one worked temporarily:

public int Test() {
return 3;
}//method

but not with such exotic methods as:

public string Test(string a, string b) {
return a + b;
}//method

this one is interesting:

public string Test() {
return "hello";
}//method

the string literal is not included in the method body IL
so that can never work ...

and some signature problems solved with:

SignatureHelper sh = SignatureHelper.GetLocalVarSigHelper();
dii.SetLocalSignature(sh.GetSignature());

I am getting the impression that the IL has to be "rebound" a bit like
rebasing a DLL

Have you had any success?

John


[quoted text, click to view]

AddThis Social Bookmark Button