2

誰かが簡単な方法を知っているのか、それともソリ​​ューション内のどのオブジェクトがシリアル化可能としてマークされていないのかを知るためのリフレクションツールを書いたのかどうか疑問に思いました。サイトをStateServerに切り替えていますが、すべてのオブジェクトをシリアル化可能としてマークする必要があります。見逃したくない。

また、2番目の部分は列挙型がシリアル化可能である必要がありますか?

このWebサイトは、VS2003で構築されたASP.NET1.1サイトです。

4

2 に答える 2

2

列挙型はシリアル化可能である必要があります。

シリアル化できないものを見つけるために、これらのオブジェクトに対して単体テストを実行します。このオブジェクトは、単にメソッドを呼び出して、シリアル化可能かどうかを確認します。このメソッドはシリアル化を試みますが、失敗した場合、オブジェクトはそうではありません...

    public static Stream serialize<T>(T objectToSerialize)
    {
        MemoryStream mem = new MemoryStream();
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(mem, objectToSerialize);
        return mem;
    }

ユニットテストでは、serialize(objectToCheck);を呼び出す必要があります。serizlisableでない場合、例外が発生します。

于 2008-12-13T15:05:06.823 に答える
2

列挙型は本質的にシリアル化可能です。

オブジェクトから属性を取得するためにこのヘルパーを作成しました。アプリケーションの先頭に、次のコードを呼び出す行を追加できます。

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
}
于 2008-12-13T15:12:26.760 に答える