首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义控件(ascx) LoadViewState错误:无法将“System.Object[]”类型的对象强制转换为“System.Web.UI.Pair”类型

自定义控件(ascx) LoadViewState错误:无法将“System.Object[]”类型的对象强制转换为“System.Web.UI.Pair”类型
EN

Stack Overflow用户
提问于 2016-03-12 02:02:16
回答 2查看 370关注 0票数 1

我正在尝试将下面的代码片段从一个ASP.NET网站迁移到一个。

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

在这一行(在LoadViewState方法中)生成错误:base.LoadViewState(VS_all[0]);

我不明白为什么,因为方法接受对象,那么为什么它关心呢?

代码语言:javascript
运行
复制
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]);

        }
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-12 03:18:39

如果使用Reflector或JustDecompile,即使它接受对象类型,也会在内部看到LoadViewState需要对。因此,如果您重构您的保存返回:

代码语言:javascript
运行
复制
return new Pair(VS_base, VS_all);

并将LoadViewState更改为:

瓦尔帕尔

代码语言:javascript
运行
复制
ir = (Pair)savedState;
base.LoadViewState(pair.First);

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

它应该解决这个问题。取自原始源(使用JustDecompile):

LoadViewState:

代码语言:javascript
运行
复制
if (savedState != null) {
     Pair pair = (Pair)savedState;
票数 0
EN

Stack Overflow用户

发布于 2016-03-18 22:00:11

感谢布莱恩·马恩斯的解决方案。下面是包含他的建议的代码,以防有人需要类似的帮助。

代码语言:javascript
运行
复制
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]);

        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35952873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档