I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead of (Type) variable. My wrong version of this method:
public class Cast
{
public static T To(object o)
{
return (T) o;
}
}
And this is simple test:
public class A
{
public static explicit operator B(A a)
{
return new B();
}
}
public class B
{
}
A a = new A();
B b = Cast.To(a);
As you guessed, this code will fail with InvalidCastException.
Is this code fail because virtual machine doesn't know how to cast variable of type object to type B at run-time? But exception message says: "unable to cast object of type A to type B". So CLR knows about real type of variable o, why it cannot perform casting?
And here is main question: how should I rewrite method T To(object o) to fix this problem?
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)