成员'<成员名称>'无法通过实例引用访问

Member #39;lt;member namegt;#39; cannot be accessed with an instance reference(成员lt;成员名称gt;无法通过实例引用访问)
本文介绍了成员'<成员名称>'无法通过实例引用访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C#,但遇到了这个问题:

I am getting into C# and I am having this issue:

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 上有这段代码:

I have this code on a UserControl:

using MyDataLayer.Section1;

public class MyClass
{
    protected void MyMethod
    {
        MyClass.MyItem oItem = new MyClass.MyItem();
        oItem = MyClass.GetItem();
        someLiteral.Text = oItem.Property1;
    }
}

一切正常,除非我去访问 Property1.智能感知只给了我EqualsGetHashCodeGetTypeToString"作为选项.当我将鼠标悬停在 oItem.Property1 上时,Visual Studio 给出了这样的解释:

Everything works fine, except when I go to access Property1. The intellisense only gives me "Equals, GetHashCode, GetType, and ToString" as options. When I mouse over the oItem.Property1, Visual Studio gives me this explanation:

MemberMyDataLayer.Section1.MyClass.MyItem.Property1.get不能通过实例引用访问,而是用类型名称限定它

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

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

I am unsure of what this means, I did some googling but wasn't able to figure it out.

推荐答案

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

In C#, unlike VB.NET and Java, you can't access static members with instance syntax. You should do:

MyClass.MyItem.Property1

引用该属性或从 Property1 中删除 static 修饰符(这可能是您想要做的).有关 static 是,看我的其他回答.

to refer to that property or remove the static modifier from Property1 (which is what you probably want to do). For a conceptual idea about what static is, see my other answer.

这篇关于成员'<成员名称>'无法通过实例引用访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)