1
A
回答
0
亚音速论坛上有此线程,一个可能的解决方案:
http://forums.subsonicproject.com/forums/t/334.aspx
#region Overridden XML Serialization Logic
public override object NewFromXML(string xml)
{
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml(xml);
object lObject = base.NewFromXML(xml);
PropertyInfo[] propertyInfos = lObject.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
if (propertyInfo == null)
continue;
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if (xdoc.DocumentElement.Attributes[propertyInfo.Name] == null)
continue;
string textValue = xdoc.DocumentElement.Attributes[propertyInfo.Name].Value;
Type[] typeArgs = propertyInfo.PropertyType.GetGenericArguments();
if (typeArgs == null || typeArgs.Length == 0)
continue;
object typedValue = GetTypedValue(textValue, typeArgs[0]);
propertyInfo.SetValue(lObject, typedValue, null);
}
}
return lObject;
}
private object GetTypedValue(string textValue, Type type)
{
if (string.IsNullOrEmpty(textValue))
return null;
object typedValue = null;
if (type == typeof(DateTime))
typedValue = Convert.ToDateTime(textValue);
else if (type == typeof(Byte))
typedValue = Convert.ToByte(textValue);
else if (type == typeof(Int16))
typedValue = Convert.ToInt16(textValue);
else if (type == typeof(Int32))
typedValue = Convert.ToInt32(textValue);
else if (type == typeof(Int64))
typedValue = Convert.ToUInt64(textValue);
else if (type == typeof(Double))
typedValue = Convert.ToDouble(textValue);
else if (type == typeof(Single))
typedValue = Convert.ToSingle(textValue);
else if (type == typeof(Boolean))
typedValue = Convert.ToBoolean(textValue);
else if (type == typeof(Guid))
typedValue = new Guid(textValue);
else
throw new NotImplementedException(string.Format("Conversion of type {0} from a string to a typed value is not implemented, yet. TextValue: {1}", type, textValue));
return typedValue;
}
public override string ToXML()
{
string xml = base.ToXML();
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml(xml);
PropertyInfo[] propertyInfos = this.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
object val = propertyInfo.GetValue(this, null);
if (val == null)
continue;
XmlAttribute attribute = xdoc.CreateAttribute(propertyInfo.Name);
attribute.Value = val.ToString();
xdoc.DocumentElement.Attributes.Append(attribute);
Console.WriteLine(propertyInfo.Name);
}
}
return xdoc.DocumentElement.OuterXml;
}
#endregion
相关问题
- 1. 亚音速2.2/2.1的bug
- 2. 亚音速2.2源代码
- 3. 亚音速2.2和ASP.NET的GridView
- 4. 异常亚音速2.2,SQLite和迁移
- 5. 问题关于亚音速2.2版
- 6. 亚音速2.2 LEFT JOIN错误
- 7. 帮助亚音速2.2结合
- 8. 实现IDataErrorInfo的亚音速2.2
- 9. 亚音速自定义集合类型
- 10. 亚音速2.2 SqlQuery类与内部连接和在哪里
- 11. 亚音速
- 12. 亚音速3
- 13. 亚音速2.2:更新仅适用于某些字段?
- 14. 无法插入使用记录(SqlServer的)亚音速2.2
- 15. 亚音速2.2 VARCHAR2最大长度问题
- 16. 批量插入到不同的供应商亚音速2.2
- 17. 亚音速2.2内连接两个数据库
- 18. 简单查询使用亚音速2.2与Oracle.dataclient错误
- 19. 亚音速2.2不能解决如何做我的加入
- 20. 亚音速收集
- 21. 亚音速2.1:SqlQuery.ExecuteJoinedDataSet()
- 22. 亚音速.Filter()在
- 23. 亚音速版本
- 24. SQLite和亚音速
- 25. 亚音速3 - SimpleRepository
- 26. 亚音速与webservice
- 27. 亚音速SimpleRepository类注入控制器
- 28. 亚音速3不产生类
- 29. 只生成修改表类 - 亚音速
- 30. 序列化亚音速类到JSON