我想知道是否有人知道一种快速方法,或者是否有人编写了一个反射工具来告诉解决方案中的哪些对象未标记为可序列化。我正在将站点切换到 StateServer,并且需要将所有对象标记为可序列化。我不想错过任何一个。
另外,第二部分枚举必须是可序列化的吗?
该网站是一个使用 VS 2003 构建的 ASP.NET 1.1 网站
最佳答案
枚举本质上是可序列化的。
我编写了这个帮助程序来获取对象的属性,您可以在应用程序的顶部添加一行来调用以下代码:
var assemblies = GetTheAssembliesFromYourApp();
foreach (var assembly in assemblies)
{
var types = assembly.GetTypes ();
foreach (var type in types)
{
if (AttributeHelper.GetAttrbiute<Serializable> (type) == null)
{
// Log somewhere - this type isn't serialisable...
}
}
}
static class AttributeHelper
{
#region Static public methods
#region GetAttribute
static public T GetAttribute<T> (object obj)
where T : Attribute
{
if (obj == null)
throw new ArgumentNullException ("obj");
// If the object is a member info then we can use it, otherwise it's an instance of 'something' so get it's type...
var member = (obj is System.Reflection.MemberInfo) ? (System.Reflection.MemberInfo)obj : obj.GetType ();
return GetAttributeImpl<T> (member);
}
#endregion GetAttribute
#endregion Static public methods
#region Static methods
#region GetAttributeImpl
static T GetAttributeImpl<T> (System.Reflection.MemberInfo member)
where T : Attribute
{
var attribs = member.GetCustomAttributes (typeof (T), false);
if (attribs == null || attribs.Length == 0)
return null;
return attribs[0] as T;
}
#endregion GetAttributeImpl
#endregion Static methods
}
关于c# - 查找不[可序列化]的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/365291/