jasonmartin
asked on
Reflect an Assembly in a different AppDomain
I am developing a plugin system -- much like you have seen posted before. So I need to be able to load a plugin (assembly) from a different folder then the running app and load into into a separate appdomain (therefor I can have mutliple version and/or unload without restartign the program). -- But this isn't the real tricky part, I can do this part using MarchByRef and Creating the new AppDomain.CreateInstanceAn dUnwrap. The problem lies is that this approach requires that I know the class name. Unfortuantely, I dont know the class names, because of the unique nature of this application there is 1000s of different classes that they can augment. The class has an custom attribute that defines is role. So what I need to be able to do is just open the assembly in that other appDomain and do .GetTypes().
If I did it in the current AppDomain I could do something like:
Assembly oAssembly = Assembly.LoadFrom(sAssembl yPath);
//Loop Through Classes creatign new packages
foreach (Type oType in oAssembly.GetTypes())
{
//if Type is an object class
if (oType.IsClass)
{
object[] oAttributes = oType.GetCustomAttributes( typeof(Job Studio.Dat a.Definiti onAttribut e), true);
...
However, now I am trying to do:
//Get Assembly Name
System.Reflection.Assembly Name oAssemblyName = System.Reflection.Assembly Name.GetAs semblyName (AssemblyP ath);
//Load Assembly
// Set up the Evidence
Evidence Evidence = new Evidence(AppDomain.Current Domain.Evi dence);
//Setup Info
AppDomainSetup AppDomainSetup = new AppDomainSetup();
AppDomainSetup.Application Base = "file://" + FolderPath;
AppDomainSetup.PrivateBinP ath = "file://" + FolderPath;
AppDomainSetup.Application Name = sPackageGuid;
// Create the AppDomain
AppDomain AssemblyAppDomain = AppDomain.CreateDomain(
sNewAppName, //AppDomain Name
Evidence, //Security Info
AppDomainSetup //AppDomain SetupInfo
);
//Load type-casted Engine
System.Reflection.Assembly oAssembly = AssemblyAppDomain.Load(oAs semblyName .FullName) ;
//Loop Through Classes creatign new packages
foreach (Type oType in oAssembly.GetTypes())
{
//if Type is an object class
if (oType.IsClass)
{
object[] oAttributes = oType.GetCustomAttributes( typeof(Job Studio.Dat a.Definiti onAttribut e), true);
However I get an "FileNotFoundError" trying to load the assembly because it is still tryign to load it in the main AppDomain context.
Thanks.
If I did it in the current AppDomain I could do something like:
Assembly oAssembly = Assembly.LoadFrom(sAssembl
//Loop Through Classes creatign new packages
foreach (Type oType in oAssembly.GetTypes())
{
//if Type is an object class
if (oType.IsClass)
{
object[] oAttributes = oType.GetCustomAttributes(
...
However, now I am trying to do:
//Get Assembly Name
System.Reflection.Assembly
//Load Assembly
// Set up the Evidence
Evidence Evidence = new Evidence(AppDomain.Current
//Setup Info
AppDomainSetup AppDomainSetup = new AppDomainSetup();
AppDomainSetup.Application
AppDomainSetup.PrivateBinP
AppDomainSetup.Application
// Create the AppDomain
AppDomain AssemblyAppDomain = AppDomain.CreateDomain(
sNewAppName, //AppDomain Name
Evidence, //Security Info
AppDomainSetup //AppDomain SetupInfo
);
//Load type-casted Engine
System.Reflection.Assembly
//Loop Through Classes creatign new packages
foreach (Type oType in oAssembly.GetTypes())
{
//if Type is an object class
if (oType.IsClass)
{
object[] oAttributes = oType.GetCustomAttributes(
However I get an "FileNotFoundError" trying to load the assembly because it is still tryign to load it in the main AppDomain context.
Thanks.
ASKER
Thats what I want to do, but it must load into a different AppDomain. Can you provide some examples of how you did this.
ASKER
Do you have any ideas/examples on how to do this? Is it possible?
I'm sorry mate, I don't have much spare time to do a concept project.
Have you tried to use the Activator to instantiate the object on the AppDomain and then iterate through its types?
Have a look: http://msdn.microsoft.com/en-us/library/ms224132.aspx
Please get back if you need any further assistance.
It would be helpful if you could create the concept project so I could try to fix it in a short amount of time.
Cheers!
Alex
Have you tried to use the Activator to instantiate the object on the AppDomain and then iterate through its types?
Have a look: http://msdn.microsoft.com/en-us/library/ms224132.aspx
Please get back if you need any further assistance.
It would be helpful if you could create the concept project so I could try to fix it in a short amount of time.
Cheers!
Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This all thing is done using Reflection... tell me if you need any help on this.
Cheers,
Alex