Is there a way in C# to cast an object based on a string?
Example,
String typeToCast = control.GetType().Name;
Button b = (typeToCast)control;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, you can’t do that. Also, what would you achieve, as you have to assign it to “static” type, in your case, it’s Button – So why not just cast it normally:
You can hovewer, check if your control is of a type:
Edit:
To create an object without having it type at compile time, you can use Activator class: