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 ");
}
}
}