如何调用对象class函数

How to call object class function

我想将 class 转换为 json 字符串,如 newtonsoft.json do

但是我无法调用函数,我该怎么办? 首先,我给 class 函数名称,然后将它们放在字符串中,但不调用 class 函数

using Newtonsoft.Json;
..
string output = JsonConvert.SerializeObject(v);

  class Data
    {
        public int Number { get; set; }
        public string Name { get; set; }
        public int Date { get; set; }
    }
 class lib
    {
        public String verGulum(object data)
        {
            List<string> fuctionName = GetPropertiesNameOfClass(data);
            string json = "{";
            for(int i=0;i< fuctionName.Count;i++)
            {
   == Thisproblem==          json += fuctionName[i]+":"+data.GetType()+",";
            }
            json += "}";

            return json;
        }
// all function name i take 
        public List<string> GetPropertiesNameOfClass(object pObject)
        {
            List<string> propertyList = new List<string>();
            if (pObject != null)
            {
                foreach (var prop in pObject.GetType().GetProperties())
                {
                    propertyList.Add(prop.Name);
                }
            }
            return propertyList;
        }
    }
main{
lib a = new lib();
            Data d = new Data();
            d.Number = 1;
            d.Name = "AAA";
            d.Date = 15;
            Console.WriteLine(a.verGulum(d));
}

输出: {人数,JsonConvert.Data,姓名:...

我写了替代你的代码。

public string alGulum(object data)
{

    Type myType = data.GetType();
    IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
    string json = "{";
    foreach (PropertyInfo prop in props)
    {

        object propValue = prop.GetValue(data, null);
        json += "\"" + prop.Name + "\"";
        if (prop.PropertyType == typeof(string))
        {
            json +=":\"" + propValue + "\",";
        }
        else
        {
            json +=":" + propValue + ",";
        }
    }
    json += "}";
    return json;
}