In. net, through reflection, you canProgramSet to perform many operations. Now I want to dynamically load the Assembly and instantiate a class object in the Assembly. Then, the obtained object is converted to a specific class object through forced conversion to call its method.
The Assembly I want to load is very simple. There is only one class, and there is only one public attribute and one public method in the class.CodeAs follows:
Using System;
Namespace Autoobject
{
Public Class Testobject
{
Public String Name;
Public Testobject ()
{
Name ="Wayfarer";
}
Public Void Print ( String S)
{
Name=S;
Console. writeline (name );
}
}
}
The Assembly name is autoobject. dll. The path is assumed to be "E :\". The namespace is autoobject and the class name is testobject.
Then I write a console application to dynamically load it:
Using System;
Using System. reflection;
Namespace Usereflector
{
Class Class1
{
[Stathread]
Static Void Main ( String [] ARGs)
{
Object OBJ = Createobject ( @" E: \ autoobject. dll " );
Autoobject. testobject Test = (Autoobject. testobject) OBJ;
Test. Print ( " I love you! " );
Console. Readline ();
}
Public Static Object Createobject ( String Assemblyfile)
{
Assembly = Assembly. loadfrom (assemblyfile );
Type type = Assembly. GetType ( " Autoobject. testobject " );
Object OBJ = Activator. createinstance (type );
Return OBJ;
}
}
}
The main method is Createobject ().
First, load the Assembly through Assembly. loadfrom (), and then get the object type of the specified class name through the GetType () method. The name of type is autoobject. testobject.
Then, activator. createinsatance (type) is used to call the constructor to obtain the instance of the Class Object and return the object type object.
Then, in Main (), forcibly convert the object created by the preceding method to the autoobject. testobject type. Of course, I manually added a reference to autoobject. dll in the console program. Finally, call the print () method of the converted object test.
"The specified conversion is invalid ."
When I use breakpoint debugging, when the last sentence of the Createobject () method is "Return OBJ", the following information is obtained:
The OBJ value is {autoobject. testobject} and the type is system. object. The property name of the Class Object is also displayed. Its value is "wayfarer" and its type is string. Apparently it is consistent with the testobject Class Object. Why cannot it be converted?
I introduced another instance in main:
Object obj2 = new autoobject. testobject ();
Then, assign the created OBJ to obj2 and convert obj2 as follows:
Autoobject. testobject test = (autoobject. testobject) obj2;
Errors reported in the results are the same. However, after breakpoint debugging, I found that the debugging information of both objects OBJ and obj2 is the same as above. Why is the result the same as that of obj2?
However, if I change the type obtained above to get it directly from the class object, the operation will be normal.
Original: type = assembly. GetType ("autoobject. testobject ");
After modification: Type type = typeof (autoobject. testobject );
Why? In fact, the type obtained by the two methods is of the autoobject. testobject type?
The truth cannot be solved!