OpCodes.Castclass. It's necessary?

Is it necessary to issue OpCode.CastClass (typeof (A)) when you have a reference to the instance (B) over the stack, where B is the class obtained from A, in preparation for calling the method with an argument of type A?

Addition:

interface IFoo
{
    void IFoo();

}

public class A:IFoo
{
    public void IFoo()
    {

    }
}
public class B:A,IFoo
{
    new public void IFoo()
    {

    }
}

var b = new B();

(b as IFoo).Foo();
((b as A) as IFoo).Foo();
+3
source share
1 answer

I think you have something like this:

class A
{
    public void Foo() { }
}

class B : A
{
}

and you need to decide between:

B b = new B();
b.Foo();

and

B b = new B();
((A)b).Foo();

Both work. But casting is not required because it Binherits all members from A.

+2
source

All Articles