[Memo] reflection of C #

Source: Internet
Author: User

I have read a lot of C # knowledge over the past few days. I feel that I have forgotten it some time later, and I will make a close-up Memo to avoid having to start searching again.

Definition: Reflection, through which we can dynamically obtain various information at runtime, suchProgramSet, module, type, field, attribute, method, and event

 

Written after your referenceCode:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. reflection;

Namespace refelction
{
Class Program
{
Static void main (string [] ARGs)
{
String A = "", myword = "";
Test mytest;
Do
{
A = console. Readline (). tostring ();
If (A = "1 ")
{
Myword = "test1 ";
}
Else
{
Myword = "Test2 ";
}
Console. writeline (myword );
Mytest = (TEST) Assembly. Load ("refelction"). createinstance ("refelction." + myword );
}
While (A. toupper ()! = "Y ");

Assembly ass;
Type type;
Object OBJ;
Try
{
Ass = assembly. LoadFile (@ "E: \ learning Source Code \ reflection \ test3 \ bin \ debug \ test3.dll ");
Type = ass. GetType ("test3.class1"); // The namespace and class name must be used.
System. reflection. methodinfo method = type. getmethod ("test4"); // method name
OBJ = ass. createinstance ("test3.class1"); // The namespace and class name must be used.
String S = (string) method. Invoke (OBJ, new string [] {"\ n test! "}); // Call the instance method
Console. writeline (s );
Console. Readline ();
}
Catch
{

}
}
}

Public abstract class test
{
Public abstract void myprint ();
}

Public class test1: Test
{
Public override void myprint ()
{
Console. writeline ("this is test1 ");
}
}

Public class Test2: Test
{
Public override void myprint ()
{
Console. writeline ("this is Test2 ");
}
}
}

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.