我进入了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;
            }
        }
     }
 }

我在用户控件上有以下代码:

using MyDataLayer.Section1;

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

一切正常,除了进入access Property1.intellisense只提供"EqualsGetHashCodeGetTypeToString"作为选项.当我将鼠标移到oItem.Property1上时,Visual Studio给了我以下解释:

Member水催化剂.第一节.我的班级.我的项目.物业1.getcannot be accessed with an instance reference, qualify it with a type name instead

我不确定这意味着什么,我在谷歌上搜索了一下,但没能找到答案.

推荐答案

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

MyClass.MyItem.Property1

引用该属性或从Property1中删除static修饰符(这可能是您想要做的).关于什么的概念.

Asp.net相关问答推荐

502 DotNet WebApplication的网关nginx已损坏

禁用托管优化并重新启动调试实际上在 Visual Studio 中更改了哪些设置?

授权错误错误 400:C# 上的 redirect_uri_mismatch

如何在 IIS 上配置 Web Deploy 发布功能,以便开发人员可以发布?

从 asp.net 中的 Web.config 获取连接字符串

Dotnet core 2.0 身份验证多模式身份 cookie 和 jwt

为 Web 请求实现速率限制算法的最佳方法是什么?

使用会话变量有多安全 - asp.net / c#

如果站点在 localhost 或 127.0.0.1 上运行,如何判断 ASP.NET MVC 视图

将命令行参数传递给 ASP.NET Core 中的 Startup 类

当用户使表单无效时单击取消按钮时,如何清除 MVC 客户端验证错误?

如何在 C# 中转换 TryCast?

如何判断用户代理是 ipad 还是 iphone?

svg 无法在 localhost 上的 IIS 网络服务器上运行

由 UpdatePanel 内 GridView 内的 LinkBut​​ton 触发的完整回发

如何在 ASP.NET 中设置下拉列表项?

如何告诉 RadioButtonList 不生成表格

GridView 按代码隐藏列

ASP.NET Excel 导出编码问题

使用 jQuery 从 asp:RadioButtonList 读取选定的值