在本文中,我们将讨论如何使用C#获取组件中的方法中的参数的信息。
如果我们的程序集包含包含任何参数的方法,我们希望获取它们的参数信息,那么我们将使用system.reflection命名空间的parameterinfo类,并且我们将使用getParameters方法获取该方法的参数。
通过使用反射可以实现后期绑定。
例如,在某些应用程序中,我们不知道在编译时加载哪个程序集,因此我们要求用户在运行时输入汇编名称和类型,并且应用程序可以加载程序集。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace reflection
{
class Program
{
static void Main(string[] args)
{
Assembly asm = Assembly.LoadFrom(@"D:\meghawebsite\reflassm\reflassm\bin\Debug\reflassm.dll");
Type[] type = asm.GetTypes();
foreach (Type t in type)
{
if (t.Name == "mycls")
{
MethodInfo[] minfo = t.GetMethods();
foreach (MethodInfo m in minfo)
{
if (m.Name == "show")
{
ParameterInfo[] pinfo = m.GetParameters();
foreach (ParameterInfo p in pinfo)
{
Console.WriteLine(p.Name);
Console.WriteLine(p.ParameterType.ToString());
}
}
}
}
}
Console.ReadLine();
}
}
}
日期:2020-04-11 23:03:46 来源:oir作者:oir
