A simple example of. NET reflection

Source: Internet
Author: User
(1) namespace ClassLibrarySport

{

Public abstract class Sport

{

Protected string name;

Public abstract string GetName ();

Public abstract string GetDuration ();

}

}

========================================================== ==============

(2) namespace ClassLibrarySomeSports // This project adds a reference to (1)

{

Public class Football: ClassLibrarySport. Sport

{

Public Football ()

{

Name = "Football ";

}

Public override string GetName ()

{

Return name;

}

Public override string GetDuration ()

{

Return "four 15 minute quarters ";

}

}

}

========================================================== ==============

(3) namespace ConsoleAssemblyTest // This project adds a reference to (1)

{

Class Program

{

Static void Main (string [] args)

{

Assembly assembly = Assembly. LoadFrom (@ "E: \ ClassLibrarySomeSports \

Bin \ Debug \ ClassLibrarySomeSports. dll ");

Type [] types = assembly. GetTypes ();

Console. WriteLine ("Get Type From ClassLibrarySomeSports. dll :");

For (int I = 0; I <types. Length; I)

{

Console. WriteLine (types [I]. Name );

}

// Use the GetConstructor () method to obtain the constructor of the corresponding type and construct the object of this type.

Console. WriteLine ("Use Method GetConstructor ():");

ConstructorInfo ci = types [0]. GetConstructor (new Type [0]);

ClassLibrarySport. Sport sport = (ClassLibrarySport. Sport) ci. Invoke (new object [0]);

Console. WriteLine (sport. GetName () "has" sport. GetDuration ());

// Use Activator. CreateInstance () to construct an object of this type

// Use assembly. CreateInstance () to return null ,??

Console. WriteLine ("Use Method CreateInstance ():");

ClassLibrarySport. Sport sport1 = (ClassLibrarySport. Sport)

Activator. CreateInstance (types [0]);

Console. WriteLine (sport1.GetName () "has" sport1.GetDuration ());

// Reflect the method named "GetDuration" in the specified type and execute this method using the Invoke () method.

Object objSport = Activator. CreateInstance (types [0]);

MethodInfo method = types [0]. GetMethod ("GetDuration ");

Object o = method. Invoke (objSport, new object [0]);

Console. WriteLine (o as string );

Console. Read ();

}

}

}

==========================================================

Output:

Get Type From ClassLibrarySomeSports. dll:

Football

Use Method GetConstructor ():

Football has four 15 minute quarters

Use Method CreateInstance ():

Football has four 15 minute quarters

Four 15 minute quarters

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.