At last, I finally figured it out!
Here's the code of the target assembly being loaded from the SD card as a little-endian .pe file.
- This assembly only references Microsoft.SPOT.Native and mscorlib, both of which are already loaded in the calling assembly below. This is important because assemblies are explicitly loaded. Any dependency also needs to be dynamically loaded.
- Note the bold string in Print(): it will be in the output at the very end of this post.
using System;
using System.Reflection;
using Microsoft.SPOT;
namespace ASSM
{
public class TestClass
{
public void Print()
{
Debug.Print("Hello from " + this.ToString());
}
}
}
Now for the the loader assembly:
using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.IO;
using SecretLabs.NETMF.Hardware.Netduino;
namespace AppDomainTest
{
public class Test
{
public static void Main(string[] args)
{
StorageDevice.MountSD("SD", SPI.SPI_module.SPI1, Cpu.Pin.GPIO_Pin10 );
using (FileStream assmfile = new FileStream(@"SD\assm.pe", FileMode.Open, FileAccess.Read, FileShare.None))
{
byte[] assmbytes = new byte[assmfile.Length];
assmfile.Read(assmbytes, 0, (int) assmfile.Length);
var assm = Assembly.Load(assmbytes);
var obj = AppDomain.CurrentDomain.CreateInstanceAndUnwrap("ASSM, Version=1.0.0.0", "ASSM.TestClass");
var type = assm.GetType("ASSM.TestClass");
MethodInfo mi = type.GetMethod("Print");
mi.Invoke(obj, null);
}
StorageDevice.Unmount("SD");
}
}
}
And the output:
The debugging target runtime is loading the application assemblies and starting execution.
Assembly: ASSM (1.0.0.0) (188 RAM - 328 ROM - 151 METADATA)
AssemblyRef = 8 bytes ( 2 elements)
TypeRef = 12 bytes ( 3 elements)
FieldRef = 0 bytes ( 0 elements)
MethodRef = 16 bytes ( 4 elements)
TypeDef = 8 bytes ( 1 elements)
FieldDef = 0 bytes ( 0 elements)
MethodDef = 4 bytes ( 2 elements)
Attributes = 0 bytes ( 0 elements)
TypeSpec = 0 bytes ( 0 elements)
Resources = 0 bytes ( 0 elements)
Resources Files = 0 bytes ( 0 elements)
Resources Data = 0 bytes
Strings = 28 bytes
Signatures = 15 bytes
ByteCode = 21 bytes
Total: (9848 RAM - 79908 ROM - 44518 METADATA)
AssemblyRef = 92 bytes ( 23 elements)
TypeRef = 748 bytes ( 187 elements)
FieldRef = 60 bytes ( 15 elements)
MethodRef = 940 bytes ( 235 elements)
TypeDef = 2424 bytes ( 303 elements)
FieldDef = 932 bytes ( 460 elements)
MethodDef = 3020 bytes ( 1506 elements)
DebuggingInfo = 1528 bytes
Attributes = 48 bytes ( 6 elements)
TypeSpec = 24 bytes ( 6 elements)
Resources Files = 72 bytes ( 3 elements)
Resources = 304 bytes ( 38 elements)
Resources Data = 1184 bytes
Strings = 6778 bytes
Signatures = 5626 bytes
ByteCode = 25520 bytes
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'ASSM'
Hello from ASSM.TestClass
The thread '<No Name>' (0x1) has exited with code 0 (0x0).
Done.
Waiting for debug commands...
The program '[8] Micro Framework application: Managed' has exited with code 0 (0x0).
Cheers,
-Fabien.