C # Blog Essay VII: Reflections on the first experience

Source: Internet
Author: User

This blog is mainly about the use of reflection in C # programming:

The main purpose is to allow the program to dynamically identify the plug-in and load into the main program. This feature is a useful feature.

First, we need to define an interface

Public interface IPlug {//<summary>///        Get version number//</summary>//        <returns> Returns version </returns>        string getversion ();        <summary>///        Get plugin author//</summary>//        <returns></returns>        string Getauthor ();        <summary>//Get plugin name///</summary>//        <returns></returns>        string GetName ();        <summary>//        Get program icons///</summary>//        <returns></returns>        System.Windows.Media.Imaging.BitmapImage GetImage ();        <summary>        ////When the main program is loaded, to generate user reminders        ///</summary> void Initial ();        String Getdescribe ();        UserControl getpage ();    }

This interface defines the functions that plug-ins need to implement, and these methods need to be implemented by the plug-ins themselves, and the specific method descriptions are in the comments.

The GetPage method is to invoke the plug-in's page and return.

When we write the plugin, we should write it on the plugin's page:

public partial class Viewscore:usercontrol,iplug

To inherit the interface Iplug and implement it.

That's pretty much it.

public string getversion ()        {            return "1.0.0";        }        public string Getauthor ()        {            return "Tjubox";        }        public string GetName ()        {            return "plug-in test";        }        Public BitmapImage GetImage ()        {            Commonfuncclass CFC = new Commonfuncclass ();
This is the loading picture from the resource file return CFC. Bitmaptobitmapimage (Global::P Lugin_EWebScore.Properties.Resources.logo); } public void Initial () { } public string Getdescribe () { Return "This is a plug-in test"; } Public UserControl getpage () { return this; }

The next most critical part is how to discover the presence of plugins in the main program.

 public class PlugInManager {public PlugInManager () {getpluglist ();        Getpluginfo ();        } public ArrayList plugins = new ArrayList ();        Get plugin directory string[] files = directory.getfiles (configuration.pluginroot);        list<string> dllfile = new list<string> ();        Object Selobj;                public void Getpluglist () {//Traverse file in directory foreach (string file in files) { If the file name ends with a DLL, if (file. ToUpper (). EndsWith (".                    DLL ") {Assembly ab = assembly.loadfile (file); type[] types = ab.                    GetTypes (); foreach (Type t in types) {//If he implements the Iplug interface if (t.getinte Rface ("IPlug") = null) {//Then add him to the plug-in list Plugi Ns. ADD (AB.                     CreateInstance (T.fullname));   }}}}} public observablecollection<pluginfo> Plugli        st = new Observablecollection<pluginfo> (); public void Getpluginfo () {for (int i = 0; i < plugins. Count;                    i++) {try {//Get plug-in information MethodInfo name;                    MethodInfo version;                    MethodInfo author;                    MethodInfo logo;                    MethodInfo page;                    MethodInfo desc;                    Selobj = Plugins[i];                    Type t = Selobj.gettype ();                    Name = T.getmethod ("GetName");                    Version = T.getmethod ("GetVersion");                    Author = T.getmethod ("Getauthor");                    Logo = T.getmethod ("GetImage");                    page = T.getmethod ("GetPage");                    desc = T.getmethod ("Getdescribe"); Pluginfo PI = nEW Pluginfo (); Pi. Author = Author. Invoke (selobj, NULL).                    ToString (); Pi. Plugname = name. Invoke (selobj, NULL).                    ToString (); Pi. Version = version. Invoke (selobj, NULL).                    ToString (); Pi. Plugname = name. Invoke (selobj, NULL).                    ToString (); Pi. Logo = Logo.                    Invoke (selobj, null) as System.Windows.Media.Imaging.BitmapImage; Pi. page = page.                    Invoke (selobj, null) as UserControl; Pi. Describe = desc. Invoke (selobj, NULL).                    ToString ();                Pluglist.add (PI); } Catch {}}}}

Write a manager to discover plugins and add plug-ins list

In fact, this part of the content is very simple, we load the file, through reflection to obtain information about the file:

  Assembly ab = assembly.loadfile (file);                    type[] types = ab. GetTypes ();

We can see this.

Public observablecollection<pluginfo> pluglist = new observablecollection<pluginfo> ();

This actually uses another part of the input bindings inside MVVM, so that any changes to the data source are pushed to the view layer for synchronization. This is in connection with the data binding of the previous article.

Ingenious, there is fun.

么么么?

C # Blog Essay VII: Reflections on the first experience

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.