c# - 查找不[可序列化]的项目

标签 c# asp.net reflection session-state

我想知道是否有人知道一种快速方法,或者是否有人编写了一个反射工具来告诉解决方案中的哪些对象未标记为可序列化。我正在将站点切换到 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/

相关文章:

c# - 为什么不更新我的数据?

c# - 为任何给定的 SQL Server 表选择除第一列以外的所有列

javascript - 在 ASP.NET 中按下 Enter 时触发 OnClientClick

asp.net - 自定义 Sharepoint 列表添加/编辑页面加载缓慢

c# - 性能动态与反射

c# 反射查找重载方法 wr 到继承

c# - Linq、XmlNodes、foreach 和异常

c# - 在 Xamarin Forms 中居中对齐工具栏项目

java - 在运行时获取java字段和方法描述符

asp.net - 使用 .Resx 文件作为全局应用程序消息?