In the design mode of the policy mode, you need to load the assembly information dynamically, this article through a simple example, to explain the dynamic loading of DLLs required knowledge points. Let's take a look at the little series.
In the design mode of the policy mode, you need to load the assembly information dynamically, this article through a simple example, to explain the dynamic loading of DLLs required knowledge points.
Knowledge points involved:
The AssemblyName class, which describes an assembly, is a unique identifier for the complete description of the assembly.
The Assembly class, under the System.Reflection namespace, represents an assembly, a reusable, version-free, and self-describing common language runtime application building block.
The module class expresses the reflection on the module, which describes the modular information of an assembly.
The type class, under the System namespace, represents the types declaration: class type, interface type, array type, value type, enum type, type parameter, generic type definition, and open or closed constructed generic type.
The FieldInfo class, which discovers field properties and provides access to field metadata.
The MethodInfo class, which discovers the properties of a method and provides access to the method's metadata.
The EventInfo class, which discovers the properties of the event and provides access to the event metadata.
The ConstructorInfo class, which discovers the properties of the class constructor and provides access to the constructor metadata.
The Activator class, which contains a specific method for creating an object type locally or remotely, or for obtaining a reference to an existing remote object. This class cannot be inherited.
The BindingFlags class that specifies the flags that control the binding and the member and type search methods that are performed by reflection. When you get a method, the second parameter uses the
As shown in the following:
The specific code is as follows:
Public partial class Dllloadform:form {public Dllloadform () {InitializeComponent (); private void Btnopenfile_click (object sender, EventArgs e) {OpenFileDialog ofd = new OpenFileDialog () { Multiselect=false, Filter = "Dll info|*.dll| All Files|*.* ", Initialdirectory=appdomain.currentdomain.basedirectory, title=" DLL Information ", tag=" Please select "}; if (OFD. ShowDialog () = = DialogResult.OK) {This.txtDllFile.Text = ofd. FileName; }} private void Btnloaddll_click (object sender, EventArgs e) {if (string. IsNullOrEmpty (This.txtDllFile.Text.Trim ())) {MessageBox.Show ("Please select DLL file"); Return } loaddllinfo (This.txtDllFile.Text); }///<summary>///dynamic load DLL///</summary>//<param name= "DllPath" > The path of the DLL that needs to be loaded </PARAM&G T public void Loaddllinfo (string dllpath) {if (file.exists (DllPath)) {TreeNodeCollection tvnodes = TvD Llinfo.nodes; Tvnodes.clear (); Tvnodes.add ("Dllinfo"); AssemblyName dllassemblyname = Assemblyname.getassemblyname (DllPath); Assembly dllassembly = Assembly.Load (dllassemblyname); module[] modules = Dllassembly.getmodules ();//Gets all the module information as part of the assembly type[] types = Dllassembly.gettypes ();//gets all the definitions in the Assembly Type assemblyname[] referrenceasseblies = Dllassembly.getreferencedassemblies ();//Gets assembly information for assembly reference Tvnodes[0]. Nodes.Add ("basic Information"); string dllfullname = Dllassembly.fullname; BOOL Isglobalasseblycache = dllassembly.globalassemblycache;//whether to load bool isfulltrusted = DLLASSEMBLY.ISFULLYTR from the global Assembly usted;//whether the module Manifestmodule = dllassembly.manifestmodule;//Gets the manifest module bool isreflectiononly = DLL loaded with full trust mode Whether the assembly.reflectiononly;//is loaded into the reflection-only module//Update to Node Tvnodes[0]. Nodes[0]. Nodes.Add (String. Format ("full path: {0}", Dllfullname)); Tvnodes[0]. Nodes[0]. Nodes.Add (String. Format ("Whether global assembly: {0}", Isglobalasseblycache)); Tvnodes[0]. NOdes[0]. Nodes.Add (String. Format ("Full trust: {0}", isfulltrusted)); Tvnodes[0]. Nodes[0]. Nodes.Add (String. Format ("Reflection only: {0}", isreflectiononly)); Tvnodes[0]. Nodes[0]. Nodes.Add (String. Format ("manifest module: {0}", Manifestmodule.name)); ienumerable<type> exportedtypes = dllassembly.exportedtypes;//public type collection Tvnodes[0]. Nodes.Add ("module Information"); int i = 0; foreach (var module in modules) {fieldinfo[] fields = module. GetFields ();//Returns the global field defined in the module methodinfo[] methods = module. GetMethods ();//Returns the global method defined in the module type[] mtypes = module. GetTypes ();//returns the type set defined in the module bool Isresource = module. Isresource ();//Indicates whether this module is a resource int mdstreamversion = module. mdstreamversion;//gets the version Guid of the source data stream versionId = module. moduleversionid;//gets the version ID of the module, string modulename = module. name;//gets the name of the module, removing the path of int metadatatoken = module. Metadatatoken; String scopeName = module. ScopeName; Tvnodes[0]. NODES[1]. Nodes.Add (String. Format ("Module: {0}", ModuleName)); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (String. Format ("Data flow version: {0}", mdstreamversion)); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (String. Format ("resource: {0}", Isresource)); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (String. Format ("Version id:{0}", versionId)); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (String. Format ("metadata:{0}", Metadatatoken)); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (String. Format ("scopename:{0}", ScopeName)); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (getnodes<fieldinfo> (Fields, "public field")); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (Getnodes<methodinfo> (Methods, "mehods")); Tvnodes[0]. NODES[1]. Nodes[i]. Nodes.Add (String. Format ("Types:{0}", String. Join (",", Mtypes. Select (P = p.name))); i++; } Tvnodes[0]. Nodes.Add ("type Information"); i = 0; foreach (var type in types) {TypeAttributes TypeAttributes = type. attributes;//the property associated with type stringTypeFullName = type. Fullname;//gets the fully qualified name of the type fieldinfo[] Typefields = type. GetFields ();//Gets all public fields eventinfo[] typeevents = type. GetEvents ();//Gets All public events type[] Typeinterfaces = Type. Getinterfaces ();//gets all public interfaces memberinfo[] Typemembers = type. GetMembers ();//Gets all public members methodinfo[] Typemethods = type. GetMethods ();//Gets all public methods typeinfo TypeInfo = type. GetTypeInfo ();//Returns the representation of the specified type string nameSpace = Type. Namespace; Specifies the type of namespace string typeName = type. name;//gets the name of the current member constructorinfo[] typeconstructors = type. GetConstructors ();//constructor of type tvnodes[0]. NODES[2]. Nodes.Add (String. Format ("type: {0}", TypeName)); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (String. Format ("full name: {0}", TypeFullName)); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (String. Format ("Make type name: {0}", Typeinfo.name)); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (String. Format ("namespace: {0}", NameSpace)); Tvnodes[0]. NODES[2]. Nodes[i]. NodeS.add (String. Format ("interface: {0}", String. Join (",", typeinterfaces.select (p = = p.name))); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (getnodes<fieldinfo> (Typefields, "public Field")); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (getnodes<constructorinfo> (Typeconstructors, "constructor")); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (getnodes<eventinfo> (typeevents, "event")); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (getnodes<memberinfo> (Typemembers, "member member")); Tvnodes[0]. NODES[2]. Nodes[i]. Nodes.Add (getnodes<methodinfo> (Typemethods, "public Method")); i++; }}}///<summary>//Get node by type///</summary>//<typeparam name= "T" ></typepar am>//<param name= "Lstinfos" ></param>//<param name= "name" ></param>//<return S></returns> public TreeNode getnodes<t> (t[] Lstinfos, string name) where T:memberinfo {TreeN Ode tnode = new TreeNode (nAME); foreach (Var t in Lstinfos) {tNode.Nodes.Add (t.name); } return tnode; }///<summary>///Call static Method Example///</summary>//<param name= "sender" ></param>// <param name= "E" ></param> private void Btncallstaticbyreflection_click (object sender, EventArgs e) { AssemblyName assemblyname = Assemblyname.getassemblyname ("TestAssembly.exe"); Assembly Assembly = Assembly.Load (AssemblyName); Type t = assembly. GetType ("Testassembly.program", true, true); Object o= Activator.CreateInstance (T, false); MethodInfo MethodInfo = T.getmethod ("Main", bindingflags.static| BindingFlags.Public); Methodinfo.invoke (Null,new string[][] {new string[] {"G"}}); }///<summary>///Call non-static methods///</summary>//<param name= "sender" ></param>// <param name= "E" ></param> private void Btncallfunctionbyreflection_click (object sender, EventarGS e) {assemblyname assemblyname = Assemblyname.getassemblyname ("TestAssembly.exe");//here is relative path Assembly Assem bly = Assembly.Load (AssemblyName); Type t = assembly. GetType ("Testassembly.program", true, true); Object o = Activator.CreateInstance (t, false); MethodInfo MethodInfo = T.getmethod ("testassembly", bindingflags.instance| BindingFlags.Public); Object tmp= Methodinfo.invoke (o,null); MessageBox.Show (TMP. ToString ()); } }
There are many features of the
dynamic load and reflection invocation, which cannot be enumerated and can only be studied later in the work.