1. Realize
The main method of reflection is used here. If the user wants to pass in the method name and method parameters, we need to write the function to return this information first, and then wrap it and return it to the user.
Get all classes in a certain assembly: (For me, getting the class under the current assembly is enough. To get other assembly or dll, please query other information)
public List<string> GetClass(string assembyName = null) { Assembly asm = (); var allclass = (); return (u => ).ToList(); }
Get all methods in a certain class, mainlyGetMethods()
Method:
public List<string> GetMethod(Type type,string assembyName = null) { // Test the method to obtain the specified class and remove the built-in GetType method List<MethodInfo> methods = ().Where(u=> == false) .Where(u => !("GetType")).ToList(); return (u => ).ToList(); }
Get the incoming parameter type of a certain method, I'm usingmethod
directtostring(),
To parse out the name of the input class, there should be other methods to get the name of the input class:
public List<string> GetParameter(MethodInfo methodInfo, Assembly assembly) { // Get the input class from the method and create the class var method_FullName = (); // The information of the input class cannot be found in the method, and can only be parsed from the fullname Regex reg = new Regex(@"(?is)(?<=\()(.*)(?=\))"); Match m = (method_FullName); string inputName = (".").Last(); Type input_class = (u => (inputName)).First(); PropertyInfo[] propertyInfos = input_class.GetProperties(); List<string> result = new List<string>(); foreach ( var item in propertyInfos ) { var returnType = ().ReturnType; var name = (); (name); } return result; }
You can get the attribute type of the input class.
2. Create an instance and execute the method
If you instantiate the class in radiation, it is very troublesome that there is a dependency in the class constructor. So I used the method of getting the class in dependency injection.
first,Create aIServiceProvider
static class to obtain all services in system dependency injection, and thenmain
Get and assign values in the function:
public static class ServiceHelper { public static IServiceProvider Instance { get; set; } }
existmain
Get dependency injection in:
public static void Main(string[] args) { var webhost = CreateHostBuilder(args).Build(); using ( var scope = () ) { = ; } (); }
In this way, you can avoid dependencies, directly use the classes in dependency injection to reflect the methods, and execute:
// Get the class in dependency injection var api = (do_class);
Heredo_class
It's oneType
。
Finally, useinvoke
To execute the method:
PropertyInfo[] propertyInfos = input_class.GetProperties(); Object Args = (input_class.FullName); foreach ( var item in propertyInfos ) { var returnType = ().ReturnType; var name = (); // Match type if ( ().Contains("string") ) { (Args, "test"); } else if ( ().Contains("int") ) { (Args, 1); } } // Simulate a method for executionobject[] inputArgs = new object[] { Args }; try { var result = (api, inputArgs); } catch ( Exception ex) { (); }
HereArgs
It is throughinput
The instance object set by the parameter type obtained by the class, becauseinvoke
The parameter type isobjetc[],
So you need to pack another layer.
method
It is the method to be called,api
is an instance of this class, obtained from dependency injection.
Summarize:
FinallyQuartz
I won't write about the cooperation here, you can passJobDataMap
to pass parameters. Combined againHTTP client
, can realize basic process processing. You can call internal functions orhttp
Call the interface.
This is the article about the details of C# reflection and Quartz implementation process processing. For more related C# reflection and Quartz implementation process processing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!