a type can have multiple attributes of many types. you are try to cast an arbitrary attributes to one specific one (System.ComponentModel.TypeDescriptionProviderAttribute). in your case none of the attributes are this type. you need to test the attribute type before casting or use the handy as type (which return null if not a match).
Run Time error in System.ComponentModel
Hi Team,
I see below exception in System.ComponentModel... Would you please help me to understand more on this
2 answers
Sort by: Most helpful
-
-
QiYou-MSFT 4,326 Reputation points Microsoft Vendor
2022-12-02T07:46:22.71+00:00 Hi @Earth_2
Based on the error message you gave, this is a type conversion issue.
The first thing you can use is to check if the object is compatible with a given type, and if it is compatible, it returns true, otherwise it returns false and no exception is thrown. Before type conversion, you can use is to determine whether the object is compatible with a given type, and if so, then convert. For example:string str="test"; object obj=str; if(obj is string){string str2=(string)obj};
Secondly, we can use as: for conversion between reference types, direct conversion, if the conversion is successful, it will return the converted object, if the conversion fails, return null, and do not throw an exception. For example:
string str="test"; object obj=str; string str2=obj as string; if(str2!=null){return "success"}
If you can share a little bit of the other code, I think I can pinpoint the problem more precisely.
Best Regards
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.