1 回答
TA贡献1796条经验 获得超10个赞
您只能获得第一级属性的已知类型。在您的代码中,您只能获取Debtor类型,但您还应该处理此属性以获取Address类型,例如通过递归。
private static string BatchItem_NFSerialization(INF Alldebtors)
{
Type t = Alldebtors.GetType();
var extraTypes1 = GetKnownTypes(Alldebtors);
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(t, extraTypes1);
serializer.WriteObject(ms, ((BatchItem_NF)Alldebtors));
var retVal = Encoding.Default.GetString(ms.ToArray());
return retVal;
}
}
private static IEnumerable<Type> GetKnownTypes(object property)
{
Type t = property.GetType();
var extraValues = t.GetProperties()
.Where(p => p.PropertyType.IsInterface && !p.PropertyType.IsGenericType)
.Select(p => p.GetValue(property, null))
.ToArray();
var extraTypes = extraValues.SelectMany(GetKnownTypes).ToArray();
return extraValues.Select(v => v.GetType()).Concat(extraTypes).ToArray();
}
- 1 回答
- 0 关注
- 198 浏览
添加回答
举报