例如
public ClassA
{
private IList<ClassB> _field1;
private string _field2;
public IList<ClassB> Field1{get;set;}
public string Field2{get;set;}
....
}
public ClassB
{
private string _f;
public F{set;get;}
}假设 已经封装好一个 IList<ClassA>
如何用泛型方法 遍历出这个泛型集合所有属性值,主要是Field1 的所有属性值:如F
现在方式是,用
public static string SerializeList<T>(IList<T> objList)
{
if (objList == null || objList.Count <= 0)
return null;
System.Reflection.PropertyInfo[] mPi = typeof(T).GetProperties();
string strName = "", strValue = "";
string strName1 = "", strValue1 = "";
foreach (T t in objList)
{
if (t == null)
continue;
for (int i = 0; i < mPi.Length; i++)
{
System.Reflection.PropertyInfo pi = mPi[i];
strName = pi.Name.Replace("_", "");
strValue = pi.GetValue(t, null) == null ? string.Empty : pi.GetValue(t, null).ToString();
if (pi.PropertyType.IsGenericType)
{
此处如何获取我需要的泛型对象,然后遍历出内部值
}
}
}
}
补充:由于要实现通用方法来遍历,所以用泛型方法.
各位有什么高见,小弟不胜感激
public ClassA
{
private IList<ClassB> _field1;
private string _field2;
public IList<ClassB> Field1{get;set;}
public string Field2{get;set;}
....
}
public ClassB
{
private string _f;
public F{set;get;}
}假设 已经封装好一个 IList<ClassA>
如何用泛型方法 遍历出这个泛型集合所有属性值,主要是Field1 的所有属性值:如F
现在方式是,用
public static string SerializeList<T>(IList<T> objList)
{
if (objList == null || objList.Count <= 0)
return null;
System.Reflection.PropertyInfo[] mPi = typeof(T).GetProperties();
string strName = "", strValue = "";
string strName1 = "", strValue1 = "";
foreach (T t in objList)
{
if (t == null)
continue;
for (int i = 0; i < mPi.Length; i++)
{
System.Reflection.PropertyInfo pi = mPi[i];
strName = pi.Name.Replace("_", "");
strValue = pi.GetValue(t, null) == null ? string.Empty : pi.GetValue(t, null).ToString();
if (pi.PropertyType.IsGenericType)
{
此处如何获取我需要的泛型对象,然后遍历出内部值
}
}
}
}
补充:由于要实现通用方法来遍历,所以用泛型方法.
各位有什么高见,小弟不胜感激
调用这个方法来遍历这个对象的所有property 即 SerializeList<ClassA>(ClassAList)
可是 如何获取IList<ClassB > 的每个对象属性值
我的方法什么类都是可以序列化的举个简单的例子
interface ISerializable
{
string Serialize();
} public class A : ISerializable
{
public List<int> x { get; set; }
public string y { get; set; } public string Serialize()
{
StringBuilder r = new StringBuilder();
r.Append("{");
r.Append("\"x\":[");
foreach (int n in x)
{
r.AppendFormat("{0},", n);
}
if (x.Count > 0)
{
r.Remove(r.Length - 1, 1);
}
r.Append("]");
r.AppendFormat(",\"y\":\"{0}\"", y);
r.Append("}");
return r.ToString();
}
}
如果你再说行不通我也不知道说啥好了。。
{
}当然,如果你的ClassA里还有其他自定义类型的属性,那么你将会传入更多的T2,T3之类的。。在数据结构位置的情况下,反射是无法做到的!
if (pi.PropertyType.IsGenericType)
{
此处如何获取我需要的泛型对象,然后遍历出内部值
}
这里面即使你能获取属性,也达不到任何效果(我可以告诉你如何获取,但没意义),因为对于IList<T>,IDictionary<TKey,TValue>这两种类型,必须单独处理,也就是判断为这样的类型后,就强制类型转换到该类型上处理,如果直接获取其属性,将得到不需要的Count、IsFixedSize、IsReadOnly等属性,而你实际只需要那个Item属性即可遍历所有元素了。
需求:想用一个方法统一处理 序列化各种复杂泛型对象,
问题:系统自带的序列化方法:
1.如果遇到空值就会有空标签,但是不想有.
2.如果想动态指定只序列化对象的某一些属性,而不是全部([XmlIgnore]只能定死不序列化谁,但现在需要动态指定),好像也不能实现.
希望有方法解决
public class MyClass<T>
{
public T 属性1{get;set;}
}
你肯定是要问,这个泛型类如何获取其所有属性?这难道和普通类有区别吗?MyClass<string> obj = new MyClass<string>();
obj.属性1 = "测试值";
object property = obj.GetType().GetProperties()[0].GetValue(obj, null);
Type propertyType = property.GetType();这些获取方法和一般类完全没有两样,只有当你反射泛型方法的时候才需要考虑泛型类型的传递。
这三个是关键点,不说清楚就不能选择最佳序列化方式,比如上面cuitstudio_andy提到的方法,只能适用于自定义类型,系统自带类型就不能用。另外对于二进制序列化和xml序列化再或者Json序列化,方式又有差异,这些你不关注的要点却是问题的关键。
你最好把你最终目的说出来,从你问的看出,你选择的方法并非最佳方法,但是你不说出最终要实现的目的,就不能帮你选更好的方法实现。单单
if (pi.PropertyType.IsGenericType)
{
此处如何获取我需要的泛型对象,然后遍历出内部值
}
这里面即使你能获取属性,也达不到任何效果(我可以告诉你如何获取,但没意义),如何取?希望这位兄台能实践一下再说