I have a scenario in which I am adding DLLs to the GAC from actions in my C# code. I then need to do an Assembly.Load on the newly added DLL. However, since the DLL wasn't in the GAC when the process started, it returns null.
So, I see that code can be run in a different AppDomain, which would result in the DLL being available from the GAC in the separate AppDomain.
How can I return the value from the other AppDomain to my main thread?
I simply want to run:
var type = Assembly.Load(assembly).GetType(className);
And have it return from the other AppDomain to my main thread.
You'll have to play a little with .NET Remoting. Your objects loaded on the other AppDomain will need to derive from the MarshalByRefObject class (http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.aspx).
Just to save time, here's the code from that link:
using System;
using System.Reflection;
public class Worker : MarshalByRefObject
{
public void PrintDomain()
{
Console.WriteLine("Object is executing in AppDomain \"{0}\"",
AppDomain.CurrentDomain.FriendlyName);
}
}
class Example
{
public static void Main()
{
// Create an ordinary instance in the current AppDomain
Worker localWorker = new Worker();
localWorker.PrintDomain();
// Create a new application domain, create an instance
// of Worker in the application domain, and execute code
// there.
AppDomain ad = AppDomain.CreateDomain("New domain");
Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
Assembly.GetExecutingAssembly().FullName,
"Worker");
remoteWorker.PrintDomain();
}
}
/* This code produces output similar to the following:
Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With