Custom Control (ascx) LoadViewState error: Unable to cast object of type 'System.Object[]' to type 'System.Web.UI.Pair'

Custom Control (ascx) LoadViewState error: Unable to cast object of type 'System.Object[]' to type 'System.Web.UI.Pair'

我正在尝试将下面的代码片段从 ASP.NET 网站迁移到 Web 应用程序项目。

我收到以下错误:Unable to cast object of type 'System.Object[]' to type 'System.Web.UI.Pair'

此行产生错误(在 LoadViewState 方法中):base.LoadViewState(VS_all[0]);

我不明白为什么,因为方法接受的是Object,所以为什么要关心?

public partial class Skins_DownloadGridView : System.Web.UI.UserControl
{

    private const string CtlIdent = "Controls_DownloadGridView";
    private string _Cat = "NONE";

    private string _SubCat = "";
    private string _SubCatHeader;
    private string _FileNameHeader;
    private string _FileDescHeader;
    private string _ActionsHeader;
    private bool _debug = false;
    private bool _enabled = false;

    private EOL.UI.Web.GVUtility GVUtil = new EOL.UI.Web.GVUtility();
    protected override object SaveViewState()
    {
        EOL.Common.Logging.LogDebug(string.Format("{0}.SaveViewState", CtlIdent), EOL.Common.Constants.DebugCodes.UserControls);
        object VS_base = base.SaveViewState();
        object[] VS_all = new object[9];

        VS_all[0] = VS_base;
        VS_all[1] = _Cat;
        VS_all[2] = _SubCat;
        VS_all[3] = _SubCatHeader;
        VS_all[4] = _FileNameHeader;
        VS_all[5] = _FileDescHeader;
        VS_all[6] = _ActionsHeader;
        VS_all[7] = _debug;
        VS_all[8] = _enabled;
        return VS_all;

    }

    protected override void LoadViewState(object VS_saved)
    {
        EOL.Common.Logging.LogDebug(string.Format("{0}.LoadViewState", CtlIdent), EOL.Common.Constants.DebugCodes.UserControls);
        if ((VS_saved != null))
        {
            object[] VS_all = new object[]{VS_saved};

            if ((VS_all[0] != null))
                ***base.LoadViewState(VS_all[0]);*** <--- Error generated here.
            if ((VS_all[1] != null))
                _Cat = Convert.ToString(VS_all[1]);
            if ((VS_all[2] != null))
                _SubCat = Convert.ToString(VS_all[2]);
            if ((VS_all[3] != null))
                _SubCatHeader = Convert.ToString(VS_all[3]);
            if ((VS_all[4] != null))
                _FileNameHeader = Convert.ToString(VS_all[4]);
            if ((VS_all[5] != null))
                _FileDescHeader = Convert.ToString(VS_all[5]);
            if ((VS_all[6] != null))
                _ActionsHeader = Convert.ToString(VS_all[6]);
            if ((VS_all[7] != null))
                _debug = Convert.ToBoolean(VS_all[7]);
            if ((VS_all[8] != null))
                Enabled = Convert.ToBoolean(VS_all[8]);

        }
    }

}

如果您使用 Reflector 或 JustDecompile,您会在内部看到 LoadViewState 需要 Pair,即使它接受对象类型。因此,如果您将 Save 重构为 return:

return new Pair(VS_base, VS_all);

并将您的 LoadViewState 更改为:

变量 pa

ir = (Pair)savedState;
base.LoadViewState(pair.First);

var VS_all = (object[])pair.Second;
//Load

应该可以解决问题。取自原始来源(使用 JustDecompile):

加载视图状态:

if (savedState != null) {
     Pair pair = (Pair)savedState;

感谢 Brian Mains 的解决方案。下面是包含他的建议的代码,以防有人需要类似的帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
public partial class Skins_DownloadGridView : System.Web.UI.UserControl
{

    private const string CtlIdent = "Controls_DownloadGridView";
    private string _Cat = "NONE";

    private string _SubCat = "";
    private string _SubCatHeader;
    private string _FileNameHeader;
    private string _FileDescHeader;
    private string _ActionsHeader;
    private bool _debug = false;
    private bool _enabled = false;

    private UCLA.EOL.UI.Web.GVUtility GVUtil = new UCLA.EOL.UI.Web.GVUtility();
    protected override object SaveViewState()
    {
        UCLA.EOL.Common.Logging.LogDebug(string.Format("{0}.SaveViewState", CtlIdent), UCLA.EOL.Common.Constants.DebugCodes.UserControls);
        object VS_base = base.SaveViewState();
        object[] VS_all = new object[9];

        VS_all[0] = VS_base;
        VS_all[1] = _Cat;
        VS_all[2] = _SubCat;
        VS_all[3] = _SubCatHeader;
        VS_all[4] = _FileNameHeader;
        VS_all[5] = _FileDescHeader;
        VS_all[6] = _ActionsHeader;
        VS_all[7] = _debug;
        VS_all[8] = _enabled;

        //return VS_all;
        return new Pair(VS_base, VS_all); // <-- Change

    }

    protected override void LoadViewState(object VS_saved)
    {
        UCLA.EOL.Common.Logging.LogDebug(string.Format("{0}.LoadViewState", CtlIdent), UCLA.EOL.Common.Constants.DebugCodes.UserControls);
        if ((VS_saved != null))
        {
            object[] VS_all = new object[]{VS_saved};


            // *** CHANGE START ***

            Pair pair = (Pair)VS_saved;

            if ((VS_all[0] != null))
                base.LoadViewState(pair.First);

            VS_all = (Object[])pair.Second;

            // *** CHANGE  END  ***                

            if ((VS_all[1] != null))
                _Cat = Convert.ToString(VS_all[1]);
            if ((VS_all[2] != null))
                _SubCat = Convert.ToString(VS_all[2]);
            if ((VS_all[3] != null))
                _SubCatHeader = Convert.ToString(VS_all[3]);
            if ((VS_all[4] != null))
                _FileNameHeader = Convert.ToString(VS_all[4]);
            if ((VS_all[5] != null))
                _FileDescHeader = Convert.ToString(VS_all[5]);
            if ((VS_all[6] != null))
                _ActionsHeader = Convert.ToString(VS_all[6]);
            if ((VS_all[7] != null))
                _debug = Convert.ToBoolean(VS_all[7]);
            if ((VS_all[8] != null))
                Enabled = Convert.ToBoolean(VS_all[8]);

        }
    }

}