3
votes

I have an IList<IHtmlString> to which I want to add some elements. Since short time it throws an ArrayTypeMismatchException, but I have no idea why. This also happens when adding or accessing elements from HttpSessionStateBase.

public class HtmlList 
{
  private IList<IHtmlString> _listItems = new List<IHtmlString>();

  public void Add(IHtmlString listItem)
  {
    if (listItem != null)
    {
      _listItems.Add(listItem); // here ArrayTypeMismatchException is thrown
    }
  }
}

Can anybody explain this to me? And why didn't it happen during the last three years? I got the exception the first time after changing from .NET 4.0 to 4.5.

Stack Trace:

at System.Collections.Generic.List`1.Add(T item)
at MyCompany.Web.Mvc.UI.HtmlList.Add(IHtmlString element)
at MyCompany.Web.Mvc.Models.CaptchaExtensions.CaptchaImageElement.ToString()
at MyCompany.Web.Mvc.Models.CaptchaExtensions.CaptchaImageElement.Render(Int32 width, Int32 height)
at ASP.views_teilnahme_index_aspx.__RenderContent3(HtmlTextWriter __w, Control parameterContainer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

1
That sounds very odd to me. Can you reproduce it with a short but complete example?Jon Skeet
Please post the full stack-trace of the exception.Daniel Hilgarth
@Knaģis: How would a new instance make a difference? It still would be of type IList<IHtmlString>Daniel Hilgarth
I just watching serial upvotes to uncompleted question..Soner Gönül
@SonerGönül yes its interesting to see how many upvotes this question gets. The provided code cannot throw such an exception.Jehof

1 Answers

0
votes

i am assuming that IHtmlString is defined in the System.Web namespace.

this should not happen until you have IHtmlString interface defined somewhere else in your code and you are implementing a class with your IHtmlString interface and adding it to list

Please check this ,