In c#, I would like to find a way to do the following:
(cast_by_variable)some_class.some_method();
So, I would somehow like to determine the type of a child class, store the info in a variable, and then use it when the parent class use cast.
Object obj;
if (some_class is Class1)
{
obj = (Class1)some_class;
// do something with obj
}
Casting the object doesn't change it's type so you can always determine it with
some_class.GetType();
Type type = some_class.GetType();
There is no direct runtime cast in c# like what you are proposing but there is a workaround for this problem. You can do the following:
Type t = some_class_obj.GetType();
object result =t.GetMethod("some_method").Invoke(some_class_object,new object[]{parameter1,parameter2});
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.