-1
在我当前使用的序列化软件中,因此需要使用[Serializable]
属性标记所有内容。检查我的所有类是否具有可序列化属性
有没有一种简单的方法来检查这使用我的Visual Studio没有一次一个地通过它们,或只是等待它崩溃?
为了澄清,我不需要知道如何检查一个类是否可以在代码中进行序列化。我正在讨论使用IDE。
在我当前使用的序列化软件中,因此需要使用[Serializable]
属性标记所有内容。检查我的所有类是否具有可序列化属性
有没有一种简单的方法来检查这使用我的Visual Studio没有一次一个地通过它们,或只是等待它崩溃?
为了澄清,我不需要知道如何检查一个类是否可以在代码中进行序列化。我正在讨论使用IDE。
如果您想使用反射来查找未标记为[Serializable]
的类,则可以使用反射通过GetTypes获取类类型,然后仅查找那些未标记为Serializable的类。 试试这个:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ReflectOnSerializableAttr
{
class Program
{
static void Main(string[] args)
{
//use Linq
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && ((t.Attributes & TypeAttributes.Serializable) != TypeAttributes.Serializable)
select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));
Console.ReadKey();
}
}
[Serializable]
public class TestSerializableOne
{
public string SomeFunc() { return "somefunc"; }
}
public class TestForgotSerializable
{
private int _testInt = 200;
}
}
上述程序的输出:
Program
TestForgotSerializable
你可以写为 – 2014-11-22 17:57:21
的单元测试你能不能尝试和序列化所有这些,看看有没有什么工作?像@VsevolodGoloviznin所建议的单元测试 – 2014-11-22 18:03:02
我有很多类,每次添加新批次时,我担心我会忘记一个。我想知道是否可以使用反射来获取命名空间中的所有类... – Haedrian 2014-11-22 18:04:45