//c# 反射实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main( string[] args )
{
// 类型名,方法名,参数
string className = "ConsoleApplication1.A";
string methodName = "Fun";
string param = "param";
// 获取程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 取得类型,方法;定义参数
Type type = assembly.GetType(className);
MethodInfo method = type.GetMethod(methodName);
object[] parameters = new object[] { param };
// 创建实例;调用方法(传递参数,获取返回值)
object instance = assembly.CreateInstance(type.ToString());
string rt = (string)method.Invoke(instance, parameters);
// 打印输出
Console.WriteLine(rt);
Console.ReadKey();
}
}
class A
{
public string Fun(string param)
{
return "A.Fun:" + param;
}
}
}
2023-09-14 06:06:16
21KB
c#
反射
1