简体   繁体   中英

System.InvalidCastException in

 System.InvalidCastException: [A]System.Collections.Generic.List`1[UploadImages] cannot be cast to [B]System.Collections.Generic.List`1[UploadImages].

  if (ViewState["CurrentList"] != null)
            {
        ObjUpload.AddRange((List<UploadImages>)ViewState["CurrentList"]); // Getting the above error  
            }

UploadImages ObjUp = new UploadImages();

    List<UploadImages> ObjUpload = new List<UploadImages>();

    ObjUp.AlternateText = TxtAlternatetext.Text;
    if (TxtFre.Text != "")
    {

        ObjUp.frequency = Convert.ToInt16(TxtFre.Text);
    }
    ObjUp.ImageURL = FileUpload1.PostedFile.FileName;

    ObjUp.URL = TxtUrlToNavigate.Text;
    ObjUp.ID = i;

    ObjUpload.Add(ObjUp);

I have made my class [Serializable]

This code works sometimes but doesn't work sometimes

Use as operator to ensure:

List<UploadImages> uploadImages = ViewState["CurrentList"] as List<UploadImages>;

if(uploadImages != null)
{
    ObjUpload.AddRange(uploadImages); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM