不能使用實例引用訪問成員‘<Method>’我要進入C#,我有這樣的問題:namespace MyDataLayer{
namespace Section1
{
public class MyClass
{
public class MyItem
{
public static string Property1{ get; set; }
}
public static MyItem GetItem()
{
MyItem theItem = new MyItem();
theItem.Property1 = "MyValue";
return theItem;
}
}
}
}我在UserControl上有以下代碼:using MyDataLayer.Section1;public class MyClass{
protected void MyMethod
{
MyClass.MyItem oItem = new MyClass.MyItem();
oItem = MyClass.GetItem();
someLiteral.Text = oItem.Property1;
}}一切正常,除了我去訪問Property1..知音只給我“Equals, GetHashCode, GetType,和ToString“作為選項。當我在oItem.Property1,VisualStudio給了我以下解釋:MemberMyDataLayer.Section1.MyClass.MyItem.Property1.getcannot be accessed with an instance reference, qualify it with a type name instead我不知道這意味著什么,我做了一些谷歌,但沒能弄清楚。
3 回答

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
靜態實體將在一段時間前自動構造。 它的第一次使用。 靜態實體有一個分配的存儲位置,即 由所有訪問該實體的人共享。 靜態實體只能通過其類型名稱訪問,而不能通過其類型名稱訪問。 通過這種類型的實例。 與實例方法一樣,靜態方法沒有隱含的“this”參數。(因此,靜態方法的執行次數較少。 開銷-使用它們的原因之一。) 使用靜態實體時要考慮線程安全。
- 3 回答
- 0 關注
- 370 瀏覽
添加回答
舉報
0/150
提交
取消