In the previous article "assembly and reflection (I)", we focused on the basic concepts and principles of "assembly" and reflection. Some readers may think that these boring theories are useless. According to my experience, many questions were asked during the interview. Being able to thoroughly understand "assembly, reflection, etc." and use reflection flexibly in daily development is an important reference standard for excellent. NET developers. Haha.
In this section, I will use a specific instance. How to Use reflection in) C #). NET development.
First, I create a common class library project. The property, static method, instance method, and no-argument method are defined in the test class of this project. The Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ReflectorTest
{
Class Test
{
Private string name;
Public string Name {get; set ;}
/// <Summary>
/// Static method
/// </Summary>
/// <Returns> </returns>
Public static string staticMethod (string name)
{
Return name;
}
/// <Summary>
/// Instance method
/// </Summary>
/// <Param name = "name"> </param>
/// <Returns> </returns>
Public string sayHello (string name)
{
Return "hello:" + name;
}
/// <Summary>
/// Method without Parameters
/// </Summary>
/// <Returns> </returns>
Public string noParm ()
{
Return "I'm A noParm Method ";
}
}
}
After the above class library project is compiled, a DLL file is generated. Okay, we just need to use the "reflection" technology to try to see the relevant element information contained in the DLL and perform some simulated operations.
Is it a bit similar to "decompilation? Haha. In fact, the famous Reflector. NET decompilation tool uses the reflection principle. Debugging in Microsoft Visual Studio also uses reflection technology ..
The specific usage code is as follows (detailed instructions and comments are provided. Readers can see at a glance ):
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Reflection;
Namespace Demo
{
Class Program
{
Static void Main (string [] args)
{
// ReflectorInfo ();
ReflectorDemo ();
Console. ReadKey ();
}
/// <Summary>
/// Call the methods, attributes, and Members contained in the Assembly (including classes) using reflection...
/// </Summary>
Public static void reflectorDemo ()
{
Assembly ass;
Type type;
MethodInfo method;
Try
{
Ass = Assembly. LoadFile (@ "F: \ Projects \ ReflectorTest \ bin \ Debug \ ReflectorTest. DLL"); // try to load according to the physical DLL path
Type = ass. GetType ("ReflectorTest. Test"); // This type is reflected Based on the type name (note that the format is "namespace. Class Name ")
Object o = Activator. CreateInstance (type); // create an object instance of this type
Method = type. GetMethod ("sayHello"); // method for obtaining reflection (instance method)
String s = (string) method. Invoke (o, new string [] {"dinglang"}); // call the instance method
Console. WriteLine ("Return of calling instance method:" + s );
Method = type. GetMethod ("noParm"); // method without Parameters
S = (string) method. Invoke (o, null); // call a function without Parameters
Console. WriteLine ("return the result of calling the method without parameters:" + s );
// Type. GetProperties () // obtain all public attributes under this type
Method = type. GetMethod ("staticMethod"); // static function
S = (string) method. Invoke (null, new string [] {"dinglang"}); // call a static method
Console. WriteLine ("Call static method return:" + s );
// Obtain the attribute value based on the specified attribute name
Type. GetProperty ("Name"). GetValue (o, null );
// Set the attribute value
Type. GetProperty ("Name"). SetValue (o, "dinglang", null );
}
Catch (Exception)
{
Throw;
}
Finally
{
Ass = null;
Type = null;
Method = null;
}
}
/// <Summary>
/// Obtain the Class and Class Members (methods, attributes, etc.) in the Assembly Using Reflection)
/// </Summary>
Public static void reflectorInfo ()
{
Assembly ass = Assembly. LoadFrom (@ "F: \ Projects \ ReflectorTest \ bin \ Debug \ ReflectorTest. DLL"); // load the Assembly
Module [] modules = ass. GetModules (); // Module information
Type [] types = ass. GetTypes (); // obtain all types contained in the Assembly
Foreach (var item in types)
{
Console. WriteLine ("included type Name:" + item. Name );
MethodInfo [] methods = item. GetMethods (); // gets information about methods contained in this type
Foreach (var method in methods)
{
Console. WriteLine ("Name of the method contained in this class:" + method. Name );
}
PropertyInfo [] PropertyInfo = item. GetProperties ();
Foreach (var pro in PropertyInfo)
{
Console. WriteLine ("Name of the property contained in this class:" + pro. Name );
}
}
}
}
}
You can refer to the above Code and try it by yourself. It is no exaggeration to say that when you really learn "reflection", you will fall in love with "reflection ". However, it is worth noting that reflection may consume a lot of performance. We hope that you can use it properly.
From a programmer in the programming world