Hello guys,
I was wondering if anyone can point me to an example of how to CREATE an Assembly interface and load it into another Project at Runtime…
so far i can create a Separate assembly and load it’s pe image using Assembly.Load();
i can call functions in the assembly and get data returned.
but now i want to define an interface for my Assemblies and use AppDomain to load and unload them at runtime… but i have been unsuccessful.
BTW i have visited the Pyxis 2 Project and source code but it is too tangled together that it doesn’t clearly show how it loads the assembly from File into an AppDomain.
i would really appreciate it if anyone could share a sample of two separate projects where one is the Main Project and the is the assembly that the main would load using AppDomain…
here is what i have so far:
Standard MF Class library: MFClassLibrary1
Class1.cs
using System;
using Microsoft.SPOT;
namespace MFClassLibrary1
{
[Serializable]
public class Program : System.MarshalByRefObject, IPlugin
{
public static void Main(string test)
{
try
{
Debug.Print("Hello, World! (Main method)= " + test);
}
catch (Exception)
{
//throw;
}
}
public string RemoteObject(ref string[] test)//
{
try
{
Debug.Print("Hello, World! (Assembly method)= " + test[0]);
}
catch (Exception)
{
//throw;
}
// throw new NotImplementedException();
return "Cooooool= " + test[0];
}
}
}
i build the above code and browse to the bin/debug/le folder and copy MFClassLibrary1.pe file to the SDCard… BTW this is the assembly file that you should load and not the DLL… i noticed a lot of people were having trouble loading assemblies because the were using the DLL.
and my Gadgeteer project looks like this:
void ProgramStarted()
{
sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
}
void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
{
Debug.Print("SD Card Detected");
byte[] test = File.ReadAllBytes(@ "\SD\MFClassLibrary1.pe");
Assembly a = Assembly.Load(test);
// Attempt to execute DLL
try
{
MethodInfo[] m;
Type[] t = a.GetTypes();
for (var j = 0; j < t.Length; j++)
{
m = t[j].GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
for (int i = 0; i < m.Length; i++)
{
if (m[i].Name == "RemoteObject")
Debug.Print("************returned from remote= " + (string)m[i].Invoke(asm, new object[] { new string[] { "hey i called RemoteObject and i am a String Array" } }));
else
Debug.Print("************returned from Main= " + (string)m[i].Invoke(asm, new object[] { "hey i'm calling the main Method and i am a String" }));
}
}
}
catch (Exception)
{
// Do nothing
}
}
my Questions are:
- how would i updated MFClassLibrary1 to use an interface and be able to access that interface in from my Gadgeteer project using Type Safe classes and AppDomain to share Data between the project?
- is it possible to register a Callback in the Child Assembly so it can trigger a Method from the Main application? kind of like Events?
thank you.
Jay.