How to cast Object to its actual type?

前端 未结 10 1065
余生分开走
余生分开走 2020-11-30 19:46

If I have:

void MyMethod(Object obj) {   ...   }

How can I cast obj to what its actual type is?

10条回答
  •  眼角桃花
    2020-11-30 20:40

    Implement an interface to call your function in your method
    interface IMyInterface
    {
     void MyinterfaceMethod();
    }
    
    IMyInterface MyObj = obj as IMyInterface;
    if ( MyObj != null)
    {
    MyMethod(IMyInterface MyObj );
    }
    

提交回复
热议问题