First of all, we will say that reflection is inefficient, but can most frameworks reduce reflection? Reflection can be used easily when it brings us code. If there is anything inappropriate, I hope you can point it out.
1. The project structure is as follows: a ClassLb class library project, a testReflect webform project for testing, and testReflect adds references to the ClassLb Class Library Project
2. I added two very simple classes to the ClassLb class library project. The Code is as follows:
Insert( a, a + + b + Update( a, a + + b + Insert( a, Update( a,
3. The webform project's test code in Default. aspx. cs is as follows: Note: add the namespace using System. Reflection;
Type AccessType( assemblyName, = = (assembly == Exception(= assembly.GetType(assemblyName + + (type == Exception(
ExecuteMethod( assemblyName, typeName, method, returnObject = == type.InvokeMember(method, BindingFlags.Default | BindingFlags.InvokeMethod, ,
4. The test code is as follows:
Page_Load( a = ExecuteMethod(, , , , + b = ExecuteMethod(, , , , + c = ExecuteMethod(, , , , + d = ExecuteMethod(, , , , +
5. Execution result:
6. I learned from my experiences that my previous project used Jquery to call webservice through ajax, and webservice to call the class library method. Each object has the addition, deletion, modification, and query operations, in webservice, I need to create four operation methods for ajax to call. I have written a lot of WebServices, and each webservice has several other methods, which wastes a lot of time, when I use the reflection shown above, I only need to use a webservice and a method. When ajax is called at the front end, I can input the corresponding parameters, which will greatly improve the development speed, this is just my experience. If you have a better way, please share it with you.