我正在尝试确定返回类型中涉及的字段列表(在这种情况下,来自 Ob 类的“字符串 MyName”和“字符串 MyAddress”以及它们各自的文档字符串)。
我到了获取返回类型的阶段,但我尝试的其他任何方法要么是给我空白值,要么是抛出异常。有什么建议么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SampleProf
{
class Program
{
static void Main(string[] args)
{
Sample cl = new Sample();
Type myClType = cl.GetType();
MethodInfo[] methodsInfo = myClType.GetMethods();
List<string> MethodList = new List<string>();
// Iterate through all methods
foreach (MethodInfo methodInfo in methodsInfo)
{
if (methodInfo.IsPublic && methodInfo.Name.Contains("Get") && !methodInfo.Name.Contains("GetType"))
{
if (methodInfo.ReturnType != typeof(void))
{
// Do something here?
}
}
}
Console.Read();
}
}
public class Sample
{
public Ob GetMe()
{
return null;
}
}
public class Ob
{
/// <summary>My name</summary>
string MyName;
/// <summary>My address</summary>
string MyAddress;
}
}