试图在C#中调用ArrayList索引的对象方法

rob*_*ito 2 c# methods arraylist object

我是新的C#和我正在写一个程序,我有一个ArrayList (unitArray)Unit对象,我试图调用一个non-static在引用的对象上的方法ArrayList.我尝试访问特定对象并调用它的方法,但它不起作用.我很感激帮助解决这个问题.

Unit.unitArray[selectedUnit].DisplayUnitAttributes()
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

'object' does not contain a definition for 'DisplayUnitAttributes' and no extension method 'DisplayUnitAttributes' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) 
Run Code Online (Sandbox Code Playgroud)

Val*_*mas 5

您需要将对象转换为其类型.代替下面的MyClass,替换你的实际类类型.

(Unit.unitArray[selectedUnit] as MyClass).DisplayUnitAttributes()
Run Code Online (Sandbox Code Playgroud)