繁体   English   中英

在C#中,如何单向序列化不可序列化?

[英]In C#, how do you one-way serialize the unserializable?

通常,我需要序列化一个对象,用于记录或调试。 这是一个单向序列化 - 我不需要稍后将其取回,我只需要将一个对象转换为字符串以将其写入某处。

是的,是的 - 这就是为什么你应该总是覆盖ToString方法。 我知道这个。 但我经常处理我没有写的对象,也无法改变。 另外,我不想为我编写的每个类编写和更新ToString方法。

XML序列化提供了一个看似完美的解决方案 - 只需将该对象压缩为XML。 但是有很多限制,特别是你不能序列化IDictionary,你必须有一个无参数的构造函数。 我可以在课堂上解决这些问题,但是 - 再次 - 我经常和其他人一起上课。

那么,获得对象的综合字符串表示的解决方案是什么? 有什么简单的东西我错过了吗?

如何使用您自己的逻辑扩展方法(也许是一些反射)?

public static class SerializerExtension
{
    public static String OneWaySerialize(this Object obj)
    {
        if (Object.ReferenceEquals(obj, null))
        {
            return "NULL";
        }
        if (obj.GetType().IsPrimitive || obj.GetType() == typeof(String))
        {
            if (obj is String)
                return String.Format("\"{0}\"", obj);
            if (obj is Char)
                return String.Format("'{0}'", obj);
            return obj.ToString();
        }

        StringBuilder builder = new StringBuilder();
        Type objType = obj.GetType();
        if (IsEnumerableType(objType))
        {
            builder.Append("[");

            IEnumerator enumerator = ((IEnumerable)obj).GetEnumerator();
            Boolean moreElements = enumerator.MoveNext();
            while (moreElements)
            {
                builder.Append(enumerator.Current.OneWaySerialize());
                moreElements = enumerator.MoveNext();
                if (moreElements)
                {
                    builder.Append(",");
                }
            }

            builder.Append("]");
        }
        else
        {
            builder.AppendFormat("{0} {{ ", IsAnonymousType(objType) ? "new" : objType.Name);

            PropertyInfo[] properties = objType.GetProperties();
            for (Int32 p = properties.Length; p > 0; p--)
            {
                PropertyInfo prop = properties[p-1];
                String propName = prop.Name;
                Object propValue = prop.GetValue(obj, null);
                builder.AppendFormat("{0} = {1}", propName, propValue.OneWaySerialize());
                if (p > 1)
                {
                    builder.Append(", ");
                }
            }

            builder.Append(" }");
        }

        return builder.ToString();
    }

    // http://stackoverflow.com/a/2483054/298053
    private static Boolean IsAnonymousType(Type type)
    {
        if (type == null)
        {
            return false;
        }
        return Attribute.IsDefined(type, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), false)
            && type.IsGenericType && type.Name.Contains("AnonymousType")
            && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
            && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;
    }

    private static Boolean IsEnumerableType(Type type)
    {
        if (type == null)
        {
            return false;
        }
        foreach (Type intType in type.GetInterfaces())
        {
            if (intType.GetInterface("IEnumerable") != null || (intType.IsGenericType && intType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
            {
                return true;
            }
        }
        return false;
    }
}

这样称呼它:

someDefinedObject.OneWaySerialize();

Revisisons

  1. 初始版本
  2. 更新于12.26.2012
    • 添加了对IEnumerable的检查(感谢aboveyou00)
    • 添加了对匿名类型的检查(并在输出时将其标记为“new”)

如果您喜欢序列化为JSON, Json.NET是解决此问题的绝佳解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM