SoFunction
Updated on 2025-03-06

Details of C# Reflection and Quartz Implementation Process Processing

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&lt;string&gt; GetMethod(Type type,string assembyName = null)
        {
            // Test the method to obtain the specified class and remove the built-in GetType method            List&lt;MethodInfo&gt; methods = ().Where(u=&gt; == false)
                .Where(u =&gt; !("GetType")).ToList();
            return (u =&gt; ).ToList();
        }


Get the incoming parameter type of a certain method, I'm usingmethoddirecttostring(),To parse out the name of the input class, there should be other methods to get the name of the input class:

public List&lt;string&gt; 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)(?&lt;=\()(.*)(?=\))");
            Match m = (method_FullName);
            string inputName = (".").Last();
            Type input_class = (u =&gt; (inputName)).First();
            PropertyInfo[] propertyInfos = input_class.GetProperties();
            List&lt;string&gt; result = new List&lt;string&gt;();
            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 aIServiceProviderstatic class to obtain all services in system dependency injection, and thenmainGet and assign values ​​in the function:

public static class ServiceHelper
    {
        public static IServiceProvider Instance { get; set; }

    }

existmainGet 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_classIt's oneType

Finally, useinvokeTo 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)
 {
     ();
 }

HereArgsIt is throughinputThe instance object set by the parameter type obtained by the class, becauseinvokeThe parameter type isobjetc[],So you need to pack another layer.

methodIt is the method to be called,apiis an instance of this class, obtained from dependency injection.

Summarize:

FinallyQuartzI won't write about the cooperation here, you can passJobDataMapto pass parameters. Combined againHTTP client, can realize basic process processing. You can call internal functions orhttpCall 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!