【发布时间】:2022-01-16 14:57:52
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct Test {
public string testString { get; set; }
public Test(string TestString) {
testString = TestString;
}
}
public class arrayTest : MonoBehaviour {
void Start() {
object[] array = new object[2];
Test tester = new Test("hello");
array[0] = tester;
Debug.Log(array[0].testString);
}
}
我对 C# 很陌生,如果这是一个愚蠢的问题,我很抱歉。这只是我的主程序的简化版本,但我不断收到相同的错误,即
“object”不包含“testString”的定义,并且找不到接受“object”类型的第一个参数的可访问扩展方法“testString”(您是否缺少 using 指令或程序集引用? )
如果对象不在数组中,它工作正常,但一旦它给了我这个。我也尝试过使用 TestString 但无济于事。任何帮助将不胜感激。
【问题讨论】:
-
你为什么使用
object[](c#中几乎所有东西的母类型)而不是实际类型Test[]?