We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

exception Invalid Cast Exception : Specified cast is not valid

Thread ID:

Created:

Updated:

Platform:

Replies:

29884 Jun 6,2005 11:45 AM UTC Jun 8,2005 03:09 PM UTC Windows Forms 1
loading
Tags: FAQ
arunkumar
Asked On June 6, 2005 11:45 AM UTC

The below mentioend C# code snippnet will be executed while loading an usercontrol at its design time, but it throws an exception Invalid Cast Exception : Specified cast is not valid while executing this line Screen screen = Activator.CreateInstance(type,null ) as Screen ; however, the same code is executing properly when it is deployed as run time. can anybody help me out to resolve this problem ? string asmName = listBox3.SelectedItem.ToString (); Assembly asm = (Assembly) asmList[asmName]; listBox4.Items.Clear (); foreach(Type type in asm.GetTypes()) { if(type.IsClass && type.IsSubclassOf(typeof(Screen))) { Screen screen = Activator.CreateInstance(type,null ) as Screen ; // this line thrown an exception if(!classList.Contains (type.Name )) classList.Add (type.Name, screen); else classList[type.Name] = screen;

Pop Catalin
Replied On June 8, 2005 03:09 PM UTC

just try using other overload of the CreateInstance Method try System.Activator.CreateInstance(System.Type type) >The below mentioend C# code snippnet will be executed while loading an usercontrol at its design time, but it throws an exception Invalid Cast Exception : Specified cast is not valid >while executing this line >Screen screen = Activator.CreateInstance(type,null ) as Screen ; > >however, the same code is executing properly when it is deployed as run time. > >can anybody help me out to resolve this problem ? > >string asmName = listBox3.SelectedItem.ToString (); >Assembly asm = (Assembly) asmList[asmName]; >listBox4.Items.Clear (); > >foreach(Type type in asm.GetTypes()) >{ >if(type.IsClass && type.IsSubclassOf(typeof(Screen))) > { >Screen screen = Activator.CreateInstance(type,null ) as Screen ; // this line thrown an exception > >if(!classList.Contains (type.Name )) > classList.Add (type.Name, screen); >else > classList[type.Name] = screen; >

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;