我正在寻找一种简单的方法来检查C#中的对象是否可序列化。
我们知道您可以通过实现 ISerializable 接口或将 [Serializable] 放在类的顶部来使对象可序列化。
我正在寻找的是一种快速检查方法,而无需反映类来获取它的属性。使用是语句可以快速界面。
使用@Flard的建议这是我提出的代码,尖叫是有更好的方法。
private static bool IsSerializable(T obj)
{
return ((obj is ISerializable) || (Attribute.IsDefined(typeof (T), typeof (SerializableAttribute))));
}
或者甚至更好地获取对象的类型,然后在类型上使用IsSerializable属性:
typeof(T).IsSerializable
请记住,这似乎只是我们正在处理的类,如果类包含其他类,您可能想要检查它们或尝试序列化并等待@pb指出的错误。
答案 0 :(得分:112)
Type
类IsSerializable
上有一个可爱的属性。
答案 1 :(得分:41)
您将不得不检查序列化为serializable属性的对象图中的所有类型。最简单的方法是尝试序列化对象并捕获异常。 (但这不是最干净的解决方案)。 Type.IsSerializable和检查serializalbe属性不会考虑图表。
示例
[Serializable]
public class A
{
public B B = new B();
}
public class B
{
public string a = "b";
}
[Serializable]
public class C
{
public D D = new D();
}
[Serializable]
public class D
{
public string d = "D";
}
class Program
{
static void Main(string[] args)
{
var a = typeof(A);
var aa = new A();
Console.WriteLine("A: {0}", a.IsSerializable); // true (WRONG!)
var c = typeof(C);
Console.WriteLine("C: {0}", c.IsSerializable); //true
var form = new BinaryFormatter();
// throws
form.Serialize(new MemoryStream(), aa);
}
}
答案 2 :(得分:16)
这是一个老问题,可能需要针对.NET 3.5+进行更新。如果类使用DataContract属性,Type.IsSerializable实际上可以返回false。这是我使用的一个片段,如果它发臭,请告诉我:)。
public static bool IsSerializable(this object obj)
{
Type t = obj.GetType();
return Attribute.IsDefined(t, typeof(DataContractAttribute)) || t.IsSerializable || (obj is IXmlSerializable)
}
答案 3 :(得分:8)
使用Type.IsSerializable,正如其他人所指出的那样。
可能不值得尝试反映并检查对象图中的所有成员是否可序列化。
成员可以声明为可序列化类型,但实际上是实例化为不可序列化的派生类型,如下面的设计示例所示:
[Serializable]
public class MyClass
{
public Exception TheException; // serializable
}
public class MyNonSerializableException : Exception
{
...
}
...
MyClass myClass = new MyClass();
myClass.TheException = new MyNonSerializableException();
// myClass now has a non-serializable member
因此,即使您确定您的类型的特定实例是可序列化的,您通常也不能确定所有实例都是如此。
答案 4 :(得分:6)
Attribute.IsDefined(typeof (YourClass), typeof (SerializableAttribute));
可能涉及水下反射,但最简单的方法是什么?
答案 5 :(得分:5)
这是一个3.5变体,它使用扩展方法使所有类都可用。
public static bool IsSerializable(this object obj)
{
if (obj is ISerializable)
return true;
return Attribute.IsDefined(obj.GetType(), typeof(SerializableAttribute));
}
答案 6 :(得分:2)
我在这个问题和答案here上得到了答案并对其进行了修改,以便获得不可序列化的类型列表。这样你就可以很容易地知道要标记哪些。
private static void NonSerializableTypesOfParentType(Type type, List<string> nonSerializableTypes)
{
// base case
if (type.IsValueType || type == typeof(string)) return;
if (!IsSerializable(type))
nonSerializableTypes.Add(type.Name);
foreach (var propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (propertyInfo.PropertyType.IsGenericType)
{
foreach (var genericArgument in propertyInfo.PropertyType.GetGenericArguments())
{
if (genericArgument == type) continue; // base case for circularly referenced properties
NonSerializableTypesOfParentType(genericArgument, nonSerializableTypes);
}
}
else if (propertyInfo.GetType() != type) // base case for circularly referenced properties
NonSerializableTypesOfParentType(propertyInfo.PropertyType, nonSerializableTypes);
}
}
private static bool IsSerializable(Type type)
{
return (Attribute.IsDefined(type, typeof(SerializableAttribute)));
//return ((type is ISerializable) || (Attribute.IsDefined(type, typeof(SerializableAttribute))));
}
然后你称之为......
List<string> nonSerializableTypes = new List<string>();
NonSerializableTypesOfParentType(aType, nonSerializableTypes);
运行时,nonSerializableTypes将具有该列表。除了将空List传递给递归方法之外,可能有更好的方法。如果有的话,有人会纠正我。
答案 7 :(得分:0)
异常对象可能是可序列化的,但使用的是其他异常。 这就是我刚刚使用WCF System.ServiceModel.FaultException:FaultException是可序列化的,但ExceptionDetail不是!
所以我使用以下内容:
// Check if the exception is serializable and also the specific ones if generic
var exceptionType = ex.GetType();
var allSerializable = exceptionType.IsSerializable;
if (exceptionType.IsGenericType)
{
Type[] typeArguments = exceptionType.GetGenericArguments();
allSerializable = typeArguments.Aggregate(allSerializable, (current, tParam) => current & tParam.IsSerializable);
}
if (!allSerializable)
{
// Create a new Exception for not serializable exceptions!
ex = new Exception(ex.Message);
}
答案 8 :(得分:0)
我的解决方案,在VB.NET中:
对象:
''' <summary>
''' Determines whether an object can be serialized.
''' </summary>
''' <param name="Object">The object.</param>
''' <returns><c>true</c> if object can be serialized; otherwise, <c>false</c>.</returns>
Private Function IsObjectSerializable(ByVal [Object] As Object,
Optional ByVal SerializationFormat As SerializationFormat =
SerializationFormat.Xml) As Boolean
Dim Serializer As Object
Using fs As New IO.MemoryStream
Select Case SerializationFormat
Case Data.SerializationFormat.Binary
Serializer = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Case Data.SerializationFormat.Xml
Serializer = New Xml.Serialization.XmlSerializer([Object].GetType)
Case Else
Throw New ArgumentException("Invalid SerializationFormat", SerializationFormat)
End Select
Try
Serializer.Serialize(fs, [Object])
Return True
Catch ex As InvalidOperationException
Return False
End Try
End Using ' fs As New MemoryStream
End Function
对于类型:
''' <summary>
''' Determines whether a Type can be serialized.
''' </summary>
''' <typeparam name="T"></typeparam>
''' <returns><c>true</c> if Type can be serialized; otherwise, <c>false</c>.</returns>
Private Function IsTypeSerializable(Of T)() As Boolean
Return Attribute.IsDefined(GetType(T), GetType(SerializableAttribute))
End Function
''' <summary>
''' Determines whether a Type can be serialized.
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="Type">The Type.</param>
''' <returns><c>true</c> if Type can be serialized; otherwise, <c>false</c>.</returns>
Private Function IsTypeSerializable(Of T)(ByVal Type As T) As Boolean
Return Attribute.IsDefined(GetType(T), GetType(SerializableAttribute))
End Function