Dynamic loading assembly (3)

zhaozj2021-02-16  56

Let's take a look at the dynamic method of the general reflection.

The following is an example of MS-help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemreflectionMethodbaseclassInvoketopic.htm

Public Class A

{

Public Virtual Int method () {return 0;}

}

Public Class B

{

Public Virtual Int method () {return 1;

}

Class mymethodinfo

{

Public static int main ()

{

Console.writeline ("/NREFLECTION.METHODINFO");

A mya = new a ();

B myb = new b ();

// Get the Type and methodInfo

TYPE mytypea = type.gettype ("a");

MethodInfo mymethodinfoa = mytypea.getMethod ("Method");

TYPE mytypeb = type.gettype ("b");

MethodInfo mymethodinfob = mytypeb.getMethod ("Method");

// Get and Display the Invoke Method

Console.write ("/ NFirst Method -" MyTypea.Fullname

"Returns" mymethodinfoa.invoke (mya, null);

Console.write ("/ NSECOND METHOD -" MyTypeb.Fullname

"Returns" MyMethodInfob.invoke (myb, null);

Return 0;

}

}

Below is an interface query method, an instance creates an object, and then perform an instance object.

Using system;

Public interface ipoint

{

// Return Now Class of Shix Interface

String ReturnNowClass ();

}

The above file was edited to classsuc.cs for classsuc.cs.

Using system;

Namespace Classlib1

{

Public Class Class1: iPoint

{

Public class1 ()

{

}

Public String ReturnNowClass ()

{

Return "WECLONE EXECUTE CLASSLIB1 CLASS1";

}

}

}

Add ClassSuc.dll to the above file, Class1 implements the iPoint interface

Edit file is classlib1.dll

Using system;

Namespace Classlib2

{

Public Class Class2: ipoint

{

Public class2 ()

{

}

Public String ReturnNowClass ()

{

Return "Classlib2" "Class2" "Weclone";

}

}

}

Add ClassSuc.dll to the above file, Class2 implements an iPoint interface

Editing the file for classlib2.dll

Maybe you are not tired, you may have to ask, what do you want to talk? ? ? ? Note that the above three DLL COPY is here "C: / Test" in the same path.

Using system;

Using system.reflection;

Class Loadexe

{

[Stathread]

Static void main (string [] args)

{

// use the file name to load the assembly in the capital application domain.

Assembly B;

B = askMBLY.LOADFROM (@ "c: /test/classssuc.dll");

TYPE [] mytypes = B.GETTYPES ();

// SHOW B interface ipoint

Foreach (Type T in Mytypes)

{

Console.writeline (T.Fullname);

}

MethodInfo method = mytypes [0] .getMethod ("ReturnNowClass");

// Get The Method to Call.

Assembly a;

String K = console.readline ();

/ / When input is greater than 10, the method of calling classlib1.dll may call ClassLib2 method.

IF (Convert.Toint32 (K)> 10)

A = askMBLY.LOADFROM (@ "c: /test/classlib1.dll");

Else

a = askMBLY.LOADFROM (@ "c: /test/classlib2.dll");

TYPE [] TYPES = a.gettypes ();

// Show B Classlib1.class1 or Classlib2.class2

FOREACH (Type T in Types)

{

Console.writeline (T.Fullname);

}

// Create An Instance of The HelloWorld Class.

Object obj = activator.createInstance (Types [0]);

// invoke the method.

Console.writeline (Method.Invoke (Obj, null);

Console.readline ();

}

}

Performance is:

IPoint

The input is required at this time.

13

Continue execution

Classlib1.class1

WECLONE EXECUTE CLASSLIB1 CLASS1

Enter the input

5

Continue execution

Classlib2.class2

WECLONE EXECUTE CLASSLIB2 CLASS2

What is achieved, dynamically load assemblies through the interface.

Significance: Reflective mechanism Realizes dynamic plugging, simply change the corresponding components of the configuration file and XCOPY,

You can directly customize a specific system without compiling.

Disadvantages: Slow performance impact speed

Some people have to ask, since they can dynamically load assemblies

How to display the Uninstall assembly CLR does not support uninstall assemblies but can uninstall all the assemblies contained in AppDomain. AppDomain.unload method Uninstall the specified application domain.

I still want to write an article to tell the dynamic assembly but I understand enough, and I feel that it is not big, so I don't want to learn those things (too impetuous performance), so mention here.

Dynamic assemblies are compiler or tools to run metadata and msil at runtime, and can generate portable executable (PE) files on disk (unlike the dynamic load assembly above) to define assemblies at runtime, then Run these assemblies and / or save them to disk.

Define the modules in the new program set at runtime and run these modules / or save them to disk.

Define types at runtime, create these types of instances, and call these types of methods.

Method for the assembly --- "Module ---" Type - "instance

Specifically see

MS-help: //ms.netframeworksdk.chs/cpguidenf/html/cpConemittingDynamicassemblies.htm

MS-help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemappdomainclassdefineDynamicassemblytopic.htm

Thank Bitfan (Digital World)

) Make you have such a harvest

转载请注明原文地址:https://www.9cbs.com/read-23010.html

New Post(0)