小编典典

会员''无法使用实例引用进行访问

c#

我正在使用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。智能感知只给我“ ,和”作为选项。当我将鼠标悬停在时,Visual
Studio给出了以下解释:Equals``GetHashCode``GetType``ToString``oItem.Property1

MemberMyDataLayer.Section1.MyClass.MyItem.Property1.getcannot be accessed with an instance reference, qualify it with a type name instead

我不确定这意味着什么,我做了一些谷歌搜索,但无法弄清楚。


阅读 331

收藏
2020-05-19

共1个答案

小编典典

在C#中,与VB.NET和Java不同,您无法static使用实例语法访问成员。你应该做:

MyClass.MyItem.Property1

来引用该属性或static从中删除修饰符Property1(这可能是您想做的)。有关什么static概念性的想法,请参阅我的其他答案

2020-05-19