可空类型在Subsonic 2.2中破坏ToXML() [英] Nullable types break ToXML() in subsonic 2.2

查看:47
本文介绍了可空类型在Subsonic 2.2中破坏ToXML()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用 .ToXML()时,我得到了错误:

When I call .ToXML() I get the error:

发生错误,反映了类型..."

"There was an error reflecting type..."

我有一个可为空的类型.我该怎么解决?

I have a nullable types. How can I solve it?

推荐答案

Subsonic论坛具有以下主题,并提供可能的解决方案:

The Subsonic forums have this thread, with a possible solution:

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

这篇关于可空类型在Subsonic 2.2中破坏ToXML()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆