In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "case analysis of C# plug-in architecture". In daily operation, I believe that many people have doubts about the case analysis of C# plug-in architecture. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "case analysis of C# plug-in architecture"! Next, please follow the editor to study!
C # is an object-oriented programming language. It provides the interface keyword to define the interface directly. At the same time, the System.Reflection namespace provides a series of related objects that access external assemblies. This lays a solid foundation for us to implement the plug-in architecture in C#.
Design process of C # plug-in Architecture
Okay, now we're going to put all the core code in the CSPluginKernel namespace. Build a C # class library project with VSIDE. Start our code in the namespace CSPluginKernel.
C # plug-in Architecture-Interface Design
Our program editor will open the document object being edited to the plug-in. After the program starts, each plug-in is enumerated and connected to the main program, passing the interface of the main program object. The plug-in can request the main program object or access the main program function through this interface.
According to the above requirements, we first need a main program interface:
Public interface IApplicationObject {void Alert (string msg); / generate a message void ShowInStatusBar (string msg); / / display the specified information in the status bar IDocumentObject QueryCurrentDocument (); / / get the currently used document object IDocumentObject [] QueryDocuments (); / / get all document objects / / set event handler void SetDelegate (Delegates whichOne, EventHandler targer) } / / currently only this event public enum Delegates {Delegate_ActiveDocumentChanged,} is needed.
Then there is the IDocumentObject interface. The plug-in accesses the editor object through this interface.
/ the editor object must implement this interface / public interface IDocumentObject {/ / these properties are the corresponding attribute maps of RichTextBox controls string SelectionText {get; set;} Color SelectionColor {get; set;} Font SelectionFont {get; set;} int SelectionStart {get; set;} int SelectionLength {get; set;} string SelectionRTF {get; set } bool HasChanges {get;} void Select (int start, int length); void AppendText (string str); void SaveFile (string fileName); void SaveFile (); void OpenFile (string fileName); void CloseFile ();}
This interface does not require much explanation. Here I have only implemented a few methods of the RichTextBox control, and readers can add others that may be useful.
Then, the interface of the plug-in is designed according to the behavior of the plug-in in its life cycle.
/ the plug-in of this program must implement this interface / public interface IPlugin {ConnectionResult Connect (IApplicationObject app); void OnDestory (); void OnLoad (); void Run ();} / indicates the result of the connection between the plug-in and the main program / public enum ConnectionResult {Connection_Success, Connection_Failed}
The main program first calls the Connect () method and passes the IApplicationObject to the plug-in. The plug-in does some initialization work during this process. The plug-in's OnLoad () method is then called. After that, when the main program receives a signal to call the plug-in (keyboard, mouse response), it calls the plug-in's Run () method to start the plug-in. When the program ends, its OnDestory () method is called. In this way, the life of the plug-in comes to an end.
C # plug-in architecture-load plug-in
Now you need to use the System.Refelction namespace. The program searches every file in the plugins directory when it starts. For each file, if it is a plug-in, load it with the Assembly object. Then enumerate each object in the assembly. The way to determine whether an assembly is our plug-in is to determine whether it is directly or indirectly implemented from IPlugin. Use the following function to pass the System.Type of the object enumerated from the assembly.
Private bool IsValidPlugin (Type t) {bool ret = false; Type [] interfaces = t.GetInterfaces (); foreach (Type theInterface in interfaces) {if (theInterface.FullName = = "CSPluginKernel.IPlugin") {ret = true; break;}} return ret;}
If all the conditions are met, IsValidPlugin () returns true. The program then creates the object and stores it in an ArrayList.
Plugins.Add (pluginAssembly.CreateInstance (plugingType.FullName))
At this point, the study on "case analysis of C# plug-in architecture" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.