Solved

Reflect an Assembly in a different AppDomain

Posted on 2009-05-08
5
1,135 Views
Last Modified: 2013-11-08
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.CreateInstanceAndUnwrap.  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(sAssemblyPath);

//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(JobStudio.Data.DefinitionAttribute), true);

...


However, now I am trying to do:
//Get Assembly Name
System.Reflection.AssemblyName oAssemblyName = System.Reflection.AssemblyName.GetAssemblyName(AssemblyPath);

//Load Assembly
// Set up the Evidence
Evidence Evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

//Setup Info
AppDomainSetup AppDomainSetup = new AppDomainSetup();
AppDomainSetup.ApplicationBase = "file://" + FolderPath;
AppDomainSetup.PrivateBinPath = "file://" + FolderPath;
AppDomainSetup.ApplicationName = 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(oAssemblyName.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(JobStudio.Data.DefinitionAttribute), 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.
0
Comment
Question by:jasonmartin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 24344634
I did a lot of plugin based application in the pass and my intent was always to:
  • be able to recognise any new assembly that may appear on the plugin repository
  • validate if that new assembly was in fact a new plugin and attach the it to the application
  • support more that one plugin per assembly
So for this what I do is:
  • have a configured plugin repository folder where I scan for the plugins
  • open each dll file and scan for the types (classes) in it
  • evaluate if the class implements my IPlugIn interface and if so store it on a list
This IPlugIn interface defines a signature of one or more methods that are the generic entrance for all plugins. Based on it I can, for instance, loop through them and invoke a generic Start() method and each will do its thing.

This all thing is done using Reflection... tell me if you need any help on this.

Cheers,
Alex
0
 

Author Comment

by:jasonmartin
ID: 24344792
Thats what I want to do, but it must load into a different AppDomain.  Can you provide some examples of how you did this.
0
 

Author Comment

by:jasonmartin
ID: 24474652
Do you have any ideas/examples on how to do this?  Is it possible?
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 24475880
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
0
 

Accepted Solution

by:
jasonmartin earned 0 total points
ID: 24829076
I learned that this is a limitation of .NET, that is why Microsoft has recently released the Addin SDK.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 47
c#, datatable, aspx 4 53
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 77
convert Absolute path to relative path of FTP location 7 51
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question