下面是一个使用反射的例子:

using System;
using System.Reflection;

class MyClass
{
    public void MyMethod(string str)
    {
        Console.WriteLine("MyMethod called with parameter: " + str);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Type type = typeof(MyClass);
        MethodInfo method = type.GetMethod("MyMethod");
        object instance = Activator.CreateInstance(type);

        string parameter = "Hello, world!";
        object[] parameters = new object[] { parameter };
        method.Invoke(instance, parameters);
    }
}

在这个例子中,我们定义了一个名为MyClass的类,其中包含一个名为MyMethod的方法。我们使用反射来获取该方法的信息,包括方法名称和参数类型。然后,我们创建MyClass的实例,并使用Invoke方法来调用该方法,并传递一个字符串参数。最后,该方法在控制台上输出传递的参数。

请用c# 反射 举个编程的例子

原文地址: https://www.cveoy.top/t/topic/bf4f 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录