C#获得类的方法和方法参数

时间:2023-03-08 22:55:36
C#获得类的方法和方法参数
  Type t = typeof(CommonController);

            StringBuilder str = new StringBuilder();

            MethodInfo[] methors = t.GetMethods(BindingFlags.Instance | BindingFlags.Public);
var md = t.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
str.AppendFormat("路由:" + t.Name).AppendLine("");
str.AppendFormat("描述" + md.Description).AppendLine(""); foreach (var v in methors)
{ var af = v.GetCustomAttribute<Models.Attribute.APIFunctionAttribute>();
if (af != null)
{
str.AppendFormat("\t功能标识:{0}", v.Name).AppendLine("");
str.AppendFormat("\t功能描述:{0}", af.Description).AppendLine("");
ParameterInfo[] ps = v.GetParameters();
str.AppendFormat("\t参数:总数{0}", ps.Length).AppendLine("");
foreach (ParameterInfo p in ps)
{
str.AppendFormat("\t\t名称{0}", p.Name).AppendLine("");
str.AppendFormat("\t\t类型{0}", p.ParameterType.FullName).AppendLine("");
}
}