当使用相同类型的对象时,将产生抛出异常

时间:2022-09-11 19:13:16

I was previously using static variables to hold variable data that I want to save between postbacks. I was having problems and found that the data in these variables is lost when the appdomain ends. So I did some research and decided to go with ViewStates:

我以前使用静态变量来保存要在回发之间保存的变量数据。我遇到了问题,发现当appdomain结束时,这些变量中的数据会丢失。所以我做了一些研究,决定采用viewstate:

static Dictionary<string, linkButtonObject> linkButtonDictonary;


protected void Page_Load(object sender, EventArgs e)
{
    if (ViewState["linkButtonDictonary"] != null)
    {
        linkButtonDictonary = (Dictionary<string, linkButtonObject>)ViewState["linkButtonDictonary"];
    }
    else
    {
        linkButtonDictonary = new Dictionary<string, linkButtonObject>();
    }
}

And here is the very simple class I use:

这是我使用的非常简单的类:

[Serializable]
public class linkButtonObject
{
    public string storyNumber { get; set; }
    public string TaskName { get; set; }
}

I am adding to linkButtonDictionary as a gridview is databound:

我添加了linkButtonDictionary作为gridview,它是databound:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton btn = (LinkButton)e.Row.FindControl("taskLinkButton");
        linkButtonObject currentRow = new linkButtonObject();
        currentRow.storyNumber = e.Row.Cells[3].Text;
        currentRow.TaskName = e.Row.Cells[5].Text;
        linkButtonDictonary.Add(btn.UniqueID, currentRow);
    }
}

It appears that my previous issues are resolved however a new one has arisin. Sometime when I postback I am getting this error:

看来我以前的问题已经解决,但是又出现了一个新的问题。有时,当我回发时,我得到了这个错误:

[A]System.Collections.Generic.Dictionary2[System.String,linkButtonObject] cannot be cast to [B]System.Collections.Generic.Dictionary2[System.String,linkButtonObject]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.

[一]System.Collections.Generic.Dictionary2[系统。不能将字符串,linkButtonObject,转换为[B]System.Collections.Generic.Dictionary2[System.String,linkButtonObject]。类型A源自“mscorlib, Version=4.0.0.0, Culture=中立,PublicKeyToken=b77a5c561934e089”,在“location”上下文中“loadnor”C:类型B源自“mscorlib, Version=4.0.0.0, Culture=中立,PublicKeyToken=b77a5c561934e089”,在“location”上下文中“loadnor”C:

I don't understand how there can be a casting issue when I am using the same class everywhere. What am I doing wrong and how do I fix it?

我不明白当我在任何地方都使用同一个类时,怎么可能会出现选角问题。我做错了什么,我该如何改正?

2 个解决方案

#1


3  

Thanks everyone for their input, it helped me track down the problem.

谢谢大家的建议,它帮助我找到了问题所在。

I had my simple class in the .aspx.cs page:

我在。aspx中有一个简单的类。cs页面:

[Serializable]
public class linkButtonObject
{
    public string storyNumber { get; set; }
    public string TaskName { get; set; }
}

This is why assemblies were loading twice and causing the issue.

这就是为什么程序集两次加载并导致问题的原因。

#2


2  

This looks exactly like the following issue:

这看起来就像下面的问题:

InvalidCastException when serializing and deserializing

序列化和反序列化时使castexception无效

As for the solution, it might be out of your control to handle assembly load etc.

至于解决方案,处理组装负载等可能超出您的控制范围。

An easy approach is XML Serialize/JSON Serialize your data as string and save that string in ViewState. To obtain it back you just need to reverse the process. This will take care of duplicate load issue for sure.

一种简单的方法是XML序列化/JSON序列化数据为字符串并将该字符串保存在ViewState中。要得到它,你只需要逆转这个过程。这将确保解决重复加载问题。

#1


3  

Thanks everyone for their input, it helped me track down the problem.

谢谢大家的建议,它帮助我找到了问题所在。

I had my simple class in the .aspx.cs page:

我在。aspx中有一个简单的类。cs页面:

[Serializable]
public class linkButtonObject
{
    public string storyNumber { get; set; }
    public string TaskName { get; set; }
}

This is why assemblies were loading twice and causing the issue.

这就是为什么程序集两次加载并导致问题的原因。

#2


2  

This looks exactly like the following issue:

这看起来就像下面的问题:

InvalidCastException when serializing and deserializing

序列化和反序列化时使castexception无效

As for the solution, it might be out of your control to handle assembly load etc.

至于解决方案,处理组装负载等可能超出您的控制范围。

An easy approach is XML Serialize/JSON Serialize your data as string and save that string in ViewState. To obtain it back you just need to reverse the process. This will take care of duplicate load issue for sure.

一种简单的方法是XML序列化/JSON序列化数据为字符串并将该字符串保存在ViewState中。要得到它,你只需要逆转这个过程。这将确保解决重复加载问题。