. NET Dynamic Loading Assemblies (iii)

Source: Internet
Author: User
Tags foreach define continue readline reflection domain ipoint

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

The following is an example of MS 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;

}

}

The following is an interface query method, an instance creates an object, and then executes an instance object

Using System;

public interface IPoint

{

Return to now class of Shix interface

String Returnnowclass ();

}

The above file is edited for ClassSuc.cs for ClassSuc.dll.

Using System;

Namespace CLASSLIB1

{

public class Class1:ipoint

{

Public Class1 ()

{

}

public string Returnnowclass ()

{

Return "Weclone Execute ClassLib1 Class1";

}

}

}

The ClassSuc.dll is also added to the above file, Class1 implements the IPoint interface

Edit file as ClassLib1.dll

Using System;

Namespace ClassLib2

{

public class Class2:ipoint

{

Public Class2 ()

{

}

public string Returnnowclass ()

{

Return "CLASSLIB2" + "Class2" + "Weclone";

}

}

}

The ClassSuc.dll is also added to the above file, Class2 implements the IPoint interface

Edit file as ClassLib2.dll

Perhaps you have seen and did not get tired of, you may have to ask, what exactly do you want to say???

Note that the above three DLL copy is under the same path here for "C:/test"

Using System;

Using System.Reflection;

Class Loadexe

{

[STAThread]

static void Main (string[] args)

{

Use the "file name to load" assembly into the current application domain.

Assembly b;

b = Assembly.LoadFrom (@ "C:/test/classsuc.dll");

type[] Mytypes = B.gettypes ();

The interface ipoint in show B

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 the input is greater than 10 o'clock, the ClassLib1.dll method is invoked otherwise the ClassLib2 method is called

if (Convert.ToInt32 (k) >10)

A = Assembly.LoadFrom (@ "C:/test/classlib1.dll");

Else

A = Assembly.LoadFrom (@ "C:/test/classlib2.dll");

type[] types = A.gettypes ();

Classlib1.class1 or classlib2.class2 in show B

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 ();

}

}

The execution effect is:

Ipoint

This requires input input

13

Continue to perform the display

Classlib1.class1

Weclone Execute CLASSLIB1 Class1

Require input when entering

5

Continue to perform the display

Classlib2.class2

Weclone Execute ClassLib2 Class2

What is implemented, dynamically loading an assembly through an interface.

Meaning: The reflection mechanism enables dynamic interpolation by simply changing the configuration file and the corresponding component of Xcopy.

A specific system can be customized directly without compiling

Disadvantage: Slow performance impact speed

Some people have to ask, since the assembly can be dynamically loaded

How do i show the uninstall assembly the CLR does not support uninstalling the assembly but can unload all the assemblies that AppDomain contains. The AppDomain.Unload method unloads the specified application domain.

Ben also wanted to write an article about the dynamic assembly but I don't understand enough, and feel that the significance of those things, they do not want to learn those things (too impetuous performance), so mentioned here.

A dynamic assembly is a compiler or tool that emits metadata and MSIL at run time and can generate portable executable (PE) files on disk (unlike the dynamic loading assembly above)

Define the assemblies at run time, and then run and/or save them to disk.

Define the modules in the new assembly at run time, and then run the modules and/or save them to disk.

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

ASSEMBLY---"Module---" Type--the method of example

See specifically

Ms-help://ms.netframeworksdk.chs/cpguidenf/html/cpconemittingdynamicassemblies.htm

Ms-help://ms. Vscc/ms. Msdnvs.2052/cpref/html/frlrfsystemappdomainclassdefinedynamicassemblytopic.htm

Thanks to Bitfan (a mortal in the Digital World) () to make yourself so fruitful




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.