我测试是通过的,怀疑你是否定义了属性?
static void Main(string[] args)
{
C c = new C();
c.S = "SS";
C c1 = getObject(c);
Console.WriteLine(c1.S);//SS1
Console.ReadLine();
}
class C
{
public string S { get; set; }//属性
}
static T getObject(T t)
{
string temp = "";
System.Reflection.PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo p in propertys)
{
if (p.PropertyType == typeof(string))
{
temp = p.GetValue(t, null).ToString();
p.SetValue(t, temp + "1", null);
}
}
return t;
}
我最近一致在做json和object互转,并且下了litjson的源代码,调试跟踪代码,并且看了
GetType().GetProperties()这个API的解释,只能获取到public的属性,private的属性是无法获取到的,有空你可以测试看下是否能获取到其他权限属性修饰下的属性是否能够获取
GetType().GetProperties()是获取属性,不是获取方法的吧。。