First, declare the Application Scenario:
1. If the function name is determined during compilation and the object type is determined during runtime, it is a good idea to use dynamic.
2. If the function name is determined during compilation and the object type is also determined during compilation, no reflection or dynamic is required.
3. If the function name can be determined at runtime, the function name is a string and must be completed using reflection.
So we must use the dynamic type improvement reflection in the first case: (the function name is determined during compilation and the object type is determined during runtime)
Assume that the function to be called by reflection is:
ClassLibrary
{
Demo
{
GetName ()
{
Name = "dddd ";
Name;
}
}
}
Traditional calls are generally:
Main ([] args)
{
Typeinfo =. Load ("ClassLibrary"). GetType ("ClassLibrary. Demo ");
Instance =. Load ("ClassLibrary"). CreateInstance ("ClassLibrary. Demo ");
Me = typeinfo. GetMethod ("GetName ");
Ret = me. Invoke (instance ,);
. WriteLine (ret. ToString ());
}
After improvement:
Program
{
ReflectObj =;
([] Args)
{
If (reflectObj =)
{
ReflectObj =. Load ("ClassLibrary"). CreateInstance ("ClassLibrary. Demo ");
}
Rrr = reflectObj. GetName ();
. WriteLine (rrr. ToString ());
. ReadKey ();
}
}
The step of reflecting the production function is missing, which is also very helpful for the reflection Performance Tuning later on :)