Dynamic loading of assemblies with AppDomain for more flexible vs2012 add-in development

Source: Internet
Author: User

Structure diagram

The build directory is the same as the new AppDomain base directory, ready to be generated and loaded at any time.

Interface code

    public interface FuncProcessings     {          void getfunctionmessage (funcmessage funcmessage);         string returnresult ();    }     [Serializable]   public struct FuncMessage    {         private string projectMessage;         private string _head;        private  string _body;        private string _foot;         public string head { get { return  _head; } set { _head = value; } }         public  string body { get { return _body; } set { _body =  value; } }        public string foot {  get { return _foot; } set { _foot = value; } }         public string projectmessage { get {  return projectMessage; } set { projectMessage = value;      }

The classes and structs constructed here need to be coupled with the serializable serialization feature for data transfer

Processing logic

Public class processing : marshalbyrefobject, processinginterface.funcprocessings     {        public processing ()          { }        public void  getfunctionmessage (funcmessage funcmessage)         {             string basepath = string. empty;            string activingfile =  string. empty;            string selectedtest =  string. empty;            string[] projrctstr =  funcmessage.projectmessage.split (' $ ');             if  (projrctstr.length < 3)              {                 basePath = projrctStr[0];                 activingFile = projrctStr[1];             }             if  (projrctstr.length < 4)              {                 selectedtest = projrctstr[2];             }            string message =  string. Format ("----Project name----\r\n{0}\r\n----the active file----\r\n{1}\r\n----The selected text----\r\n{2}\r\n----program HEAD----\r\n{3}\r\n----function----\r\n{4}\r\n----The end of the program----\ r \ n ",                                basePath,                                activingFile,                                selectedTest,                                FuncMessage.Head.Replace (" ",  ""),                                FuncMessage.Body.Replace (" ",  ""),                                funcmessage.foot.replace (" ",  ""));             GlobalVariables.functionString = message;         }        public string returnresult ()          {             return GlobalVariables.functionString;        }     }

class requires inheritance MarshalByRefObject for AppDomain boundary references

Add-in Program

  if  (_applicationobject != null)              {                 string projectmess = _applicationObject.ActiveWindow.Project.FullName +  ' $ '                            //Project Name                                        + _ ApplicationObject.ActiveDocument.FullName.Trim ()  +  "$"                          //file                                       +  ((TextSelection) _ applicationObject.ActiveDocument.Selection). Text.trim ();      //selected text                  string apppath = @ "C:\Users\Administrator\Desktop\work";                 string  assemblypath = @ "D:\ My Documents \visual studio 2012\projects\functest\functest\bin\debug\ AddFunc.dll ";                 Funcmessage funcmessage = new funcmessage ();                 funcMessage.ProjectMessage = projectmess;                  funcmessage.head = this. HeaderText.Text.Trim ();                 funcmessage.body = this. BodyText.Text.Trim ();                 funcmessage.foot = this. FootText.Text.Trim ();                 appdomainsetup setup = new appdomainsetup ();                 setup. applicationname =  "Modelload";                 setup. applicationbase = apppath;// appdomain.currentdomain.basedirectory;                &nbsP;setup. Privatebinpath = path.combine (appdomain.currentdomain.basedirectory,  "private");                 setup. Cachepath = setup. applicationbase;                 setup. shadowcopyfiles =  "true";                 setup. Shadowcopydirectories = setup. applicationbase;                 appdomain = appdomain.createdomain ("Modelloaddomain",  null, setup);                 string name =  assembly.loadfrom (assemblypath). GetName (). Fullname;                ifunc =  (funcprocessings) appdomain.createinstanceandunwrap (                 name,                 typeof (processing). FullName);                 Ifunc.getfunctionmessage (funcmessage);                 resultform rf = new resultform (Ifunc.returnresult ());                 rf. ShowDialog ();

Add-ins and applications are different, the working directory of the add-in is not the current working directory, although the editor will not error, but the runtime will error, so the reference structure and DLLs need to be placed in the installation directory under the Common7 IDE, where the processing base class is more appropriate, To create an instance, in fact it needs to be just a type, implemented in objects referenced across boundaries.

Setup. ApplicationBase = apppath;//AppDomain.CurrentDomain.BaseDirectory;

The normal program is to use the annotated path to create a new domain under Debug directory, due to the particularity of the add-in, here is not the real need to get the directory, so I specify the absolute path here, but it is also a good idea to designate this path as the handler debug directory.

Test results

Uncomment, generate. VS did not restart.

OK, this is the basic idea.

Dynamic loading of assemblies with AppDomain for more flexible vs2012 add-in development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.