不能通过实例引用访问成员的。

时间:2022-09-25 14:51:48

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

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

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;
    }
}

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:

除了访问Property1之外,一切都正常。智能感知只提供给我“Equals, GetHashCode, GetType和ToString”作为选项。当我鼠标移过oItem。Property1, Visual Studio给我这个解释:

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

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

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

我不确定这是什么意思,我用谷歌搜索了一下,但没能找到答案。

8 个解决方案

#1


191  

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

在c#中,不像VB。NET和Java,不能使用实例语法访问静态成员。你应该做的是:

MyClass.MyItem.Property1

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.

要引用该属性或从Property1中删除静态修饰符(这是您可能想要做的)。关于静态是什么概念的概念,请看我的另一个答案。

#2


31  

You can only access static members using the name of the type.

您只能使用类型的名称访问静态成员。

Therefore, you need to either write,

因此,你需要写,

MyClass.MyItem.Property1

Or (this is probably what you need to do) make Property1 an instance property by removing the static keyword from its definition.

或者(这可能是您需要做的)通过从其定义中删除static关键字,使Property1成为一个实例属性。

Static properties are shared between all instances of their class, so that they only have one value. The way it's defined now, there is no point in making any instances of your MyItem class.

静态属性在类的所有实例之间共享,因此它们只有一个值。它现在的定义方式,没有必要让你的MyItem类的任何实例。

#3


17  

I had the same issue - although a few years later, some may find a few pointers helpful:

我也有同样的问题——尽管几年后,有些人可能会发现一些有用的建议:

Do not use ‘static’ gratuitously!

不要无缘无故地使用“静态”!

Understand what ‘static’ implies in terms of both run-time and compile time semantics (behavior) and syntax.

理解“静态”在运行时和编译时语义(行为)和语法方面的含义。

  • A static entity will be automatically constructed some time before
    its first use.

    静态实体在第一次使用之前会自动构建一些时间。

  • A static entity has one storage location allocated, and that is
    shared by all who access that entity.

    静态实体有一个分配的存储位置,所有访问该实体的人共享这个存储位置。

  • A static entity can only be accessed through its type name, not
    through an instance of that type.

    静态实体只能通过它的类型名称访问,而不是通过该类型的实例来访问。

  • A static method does not have an implicit ‘this’ argument, as does an instance method. (And therefore a static method has less execution
    overhead – one reason to use them.)

    静态方法没有隐含的“this”参数,例如实例方法。(因此静态方法的执行开销更少——这是使用它们的一个原因。)

  • Think about thread safety when using static entities.

    在使用静态实体时考虑线程安全性。

Some details on static in MSDN:

MSDN上的一些静态细节:

#4


4  

No need to use static in this case as thoroughly explained. You might as well initialise your property without GetItem() method, example of both below:

在这个例子中没有必要使用静态的解释。如果没有GetItem()方法,您可以初始化您的属性,如下所示:

namespace MyNamespace
{
    using System;

    public class MyType
    {
        public string MyProperty { get; set; } = new string();
        public static string MyStatic { get; set; } = "I'm static";
    }
}

Consuming:

消费:

using MyType;

public class Somewhere 
{
    public void Consuming(){

        // through instance of your type
        var myObject = new MyType(); 
        var alpha = myObject.MyProperty;

        // through your type 
        var beta = MyType.MyStatic;
    }
}       

#5


1  

cannot be accessed with an instance reference

不能使用实例引用访问。

It means you're calling a STATIC method and passing it an instance. The easiest solution is to remove Static, eg:

这意味着您调用了一个静态方法并传递了一个实例。最简单的解决办法是去除静电。

public static void ExportToExcel(IEnumerable data, string sheetName) {

公共静态void ExportToExcel(IEnumerable数据,字符串sheetName) {

#6


1  

I got here googling for C# compiler error CS0176, through (duplicate) question Static member instance reference issue.

我在这里搜索c#编译器错误CS0176,通过(重复)问题静态成员实例引用问题。

In my case, the error happened because I had a static method and an extension method with the same name. For that, see Static method and extension method with same name.

在我的例子中,错误发生是因为我有一个静态方法和一个同名的扩展方法。为此,请参见静态方法和同名的扩展方法。

[May be this should have been a comment. Sorry that I don't have enough reputation yet.]

也许这应该是一个评论。对不起,我还没有足够的声誉。

#7


0  

Check whether your code contains a namespace which the right most part matches your static class name.

检查您的代码是否包含一个名称空间,该名称空间与您的静态类名相匹配。

Given the a static Bar class, defined on namespace Foo, implementing a method Jump or a property, chances are you are receiving compiler error because there is also another namespace ending on Bar. Yep, fishi stuff ;-)

给定一个静态的Bar类,在名称空间Foo上定义,实现一个方法跳转或属性,很可能您正在接收编译器错误,因为在Bar上还有另一个名称空间。是的,fishi东西;-)

If that's so, it means your using a Using Bar; and a Bar.Jump() call, therefore one of the following solutions should fit your needs:

如果是这样,那就意味着你使用了一个使用吧;而一个Bar.Jump()调用,因此下面的一个解决方案应该满足您的需要:

  • Fully qualify static class name with according namepace, which result on Foo.Bar.Jump() declaration. You will also need to remove Using Bar; statement
  • 根据namepace完全限定静态类名称,结果是Foo.Bar.Jump()声明。您还需要删除使用Bar;声明
  • Rename namespace Bar by a diffente name.
  • 通过一个diffente名称重命名名称空间条。

In my case, the foollowing compiler error occurred on a EF (Entity Framework) repository project on an Database.SetInitializer() call:

在我的例子中,foollowing编译器错误发生在一个Database.SetInitializer()调用的EF (Entity Framework)存储库项目上。

Member 'Database.SetInitializer<MyDatabaseContext>(IDatabaseInitializer<MyDatabaseContext>)' cannot be accessed with an instance reference; qualify it with a type name instead MyProject.ORM

This error arouse when I added a MyProject.ORM.Database namespace, which sufix (Database), as you might noticed, matches Database.SetInitializer class name.

当我添加一个MyProject.ORM时,这个错误就会触发。数据库名称空间,sufix(数据库),如您可能注意到的,匹配数据库。SetInitializer类名。

In this, since I have no control on EF's Database static class and I would also like to preserve my custom namespace, I decided fully qualify EF's Database static class with its namepace System.Data.Entity, which resulted on using the following command, which compilation succeed:

在这个过程中,由于我对EF的数据库静态类没有控制,并且我也想保留我的自定义命名空间,所以我决定使用它的namepace System.Data完全限定EF的数据库静态类。使用以下命令导致的实体,编译成功:

System.Data.Entity.Database.SetInitializer<MyDatabaseContext>(MyMigrationStrategy)

Hope it helps

希望它能帮助

#8


0  

I know this is an old thread, but I just spent 3 hours trying to figure out what my issue was. I ordinarily know what this error means, but you can run into this in a more subtle way as well. My issue was my client class (the one calling a static method from an instance class) had a property of a different type but named the same as the static method. The error reported by the compiler was the same as reported here, but the issue was basically name collision.

我知道这是一个老话题,但是我花了3个小时来弄清楚我的问题是什么。我通常知道这个错误的含义,但是你也可以用一种更微妙的方式来处理它。我的问题是我的客户端类(从实例类调用静态方法)具有不同类型的属性,但命名与静态方法相同。编译器报告的错误与这里报告的错误相同,但问题基本上是名称冲突。

For anyone else getting this error and none of the above helps, try fully qualifying your instance class with the namespace name. ..() so the compiler can see the exact name you mean.

对于任何其他获得此错误的人,以上都没有帮助,请尝试使用名称空间名称完全限定您的实例类。

#1


191  

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

在c#中,不像VB。NET和Java,不能使用实例语法访问静态成员。你应该做的是:

MyClass.MyItem.Property1

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.

要引用该属性或从Property1中删除静态修饰符(这是您可能想要做的)。关于静态是什么概念的概念,请看我的另一个答案。

#2


31  

You can only access static members using the name of the type.

您只能使用类型的名称访问静态成员。

Therefore, you need to either write,

因此,你需要写,

MyClass.MyItem.Property1

Or (this is probably what you need to do) make Property1 an instance property by removing the static keyword from its definition.

或者(这可能是您需要做的)通过从其定义中删除static关键字,使Property1成为一个实例属性。

Static properties are shared between all instances of their class, so that they only have one value. The way it's defined now, there is no point in making any instances of your MyItem class.

静态属性在类的所有实例之间共享,因此它们只有一个值。它现在的定义方式,没有必要让你的MyItem类的任何实例。

#3


17  

I had the same issue - although a few years later, some may find a few pointers helpful:

我也有同样的问题——尽管几年后,有些人可能会发现一些有用的建议:

Do not use ‘static’ gratuitously!

不要无缘无故地使用“静态”!

Understand what ‘static’ implies in terms of both run-time and compile time semantics (behavior) and syntax.

理解“静态”在运行时和编译时语义(行为)和语法方面的含义。

  • A static entity will be automatically constructed some time before
    its first use.

    静态实体在第一次使用之前会自动构建一些时间。

  • A static entity has one storage location allocated, and that is
    shared by all who access that entity.

    静态实体有一个分配的存储位置,所有访问该实体的人共享这个存储位置。

  • A static entity can only be accessed through its type name, not
    through an instance of that type.

    静态实体只能通过它的类型名称访问,而不是通过该类型的实例来访问。

  • A static method does not have an implicit ‘this’ argument, as does an instance method. (And therefore a static method has less execution
    overhead – one reason to use them.)

    静态方法没有隐含的“this”参数,例如实例方法。(因此静态方法的执行开销更少——这是使用它们的一个原因。)

  • Think about thread safety when using static entities.

    在使用静态实体时考虑线程安全性。

Some details on static in MSDN:

MSDN上的一些静态细节:

#4


4  

No need to use static in this case as thoroughly explained. You might as well initialise your property without GetItem() method, example of both below:

在这个例子中没有必要使用静态的解释。如果没有GetItem()方法,您可以初始化您的属性,如下所示:

namespace MyNamespace
{
    using System;

    public class MyType
    {
        public string MyProperty { get; set; } = new string();
        public static string MyStatic { get; set; } = "I'm static";
    }
}

Consuming:

消费:

using MyType;

public class Somewhere 
{
    public void Consuming(){

        // through instance of your type
        var myObject = new MyType(); 
        var alpha = myObject.MyProperty;

        // through your type 
        var beta = MyType.MyStatic;
    }
}       

#5


1  

cannot be accessed with an instance reference

不能使用实例引用访问。

It means you're calling a STATIC method and passing it an instance. The easiest solution is to remove Static, eg:

这意味着您调用了一个静态方法并传递了一个实例。最简单的解决办法是去除静电。

public static void ExportToExcel(IEnumerable data, string sheetName) {

公共静态void ExportToExcel(IEnumerable数据,字符串sheetName) {

#6


1  

I got here googling for C# compiler error CS0176, through (duplicate) question Static member instance reference issue.

我在这里搜索c#编译器错误CS0176,通过(重复)问题静态成员实例引用问题。

In my case, the error happened because I had a static method and an extension method with the same name. For that, see Static method and extension method with same name.

在我的例子中,错误发生是因为我有一个静态方法和一个同名的扩展方法。为此,请参见静态方法和同名的扩展方法。

[May be this should have been a comment. Sorry that I don't have enough reputation yet.]

也许这应该是一个评论。对不起,我还没有足够的声誉。

#7


0  

Check whether your code contains a namespace which the right most part matches your static class name.

检查您的代码是否包含一个名称空间,该名称空间与您的静态类名相匹配。

Given the a static Bar class, defined on namespace Foo, implementing a method Jump or a property, chances are you are receiving compiler error because there is also another namespace ending on Bar. Yep, fishi stuff ;-)

给定一个静态的Bar类,在名称空间Foo上定义,实现一个方法跳转或属性,很可能您正在接收编译器错误,因为在Bar上还有另一个名称空间。是的,fishi东西;-)

If that's so, it means your using a Using Bar; and a Bar.Jump() call, therefore one of the following solutions should fit your needs:

如果是这样,那就意味着你使用了一个使用吧;而一个Bar.Jump()调用,因此下面的一个解决方案应该满足您的需要:

  • Fully qualify static class name with according namepace, which result on Foo.Bar.Jump() declaration. You will also need to remove Using Bar; statement
  • 根据namepace完全限定静态类名称,结果是Foo.Bar.Jump()声明。您还需要删除使用Bar;声明
  • Rename namespace Bar by a diffente name.
  • 通过一个diffente名称重命名名称空间条。

In my case, the foollowing compiler error occurred on a EF (Entity Framework) repository project on an Database.SetInitializer() call:

在我的例子中,foollowing编译器错误发生在一个Database.SetInitializer()调用的EF (Entity Framework)存储库项目上。

Member 'Database.SetInitializer<MyDatabaseContext>(IDatabaseInitializer<MyDatabaseContext>)' cannot be accessed with an instance reference; qualify it with a type name instead MyProject.ORM

This error arouse when I added a MyProject.ORM.Database namespace, which sufix (Database), as you might noticed, matches Database.SetInitializer class name.

当我添加一个MyProject.ORM时,这个错误就会触发。数据库名称空间,sufix(数据库),如您可能注意到的,匹配数据库。SetInitializer类名。

In this, since I have no control on EF's Database static class and I would also like to preserve my custom namespace, I decided fully qualify EF's Database static class with its namepace System.Data.Entity, which resulted on using the following command, which compilation succeed:

在这个过程中,由于我对EF的数据库静态类没有控制,并且我也想保留我的自定义命名空间,所以我决定使用它的namepace System.Data完全限定EF的数据库静态类。使用以下命令导致的实体,编译成功:

System.Data.Entity.Database.SetInitializer<MyDatabaseContext>(MyMigrationStrategy)

Hope it helps

希望它能帮助

#8


0  

I know this is an old thread, but I just spent 3 hours trying to figure out what my issue was. I ordinarily know what this error means, but you can run into this in a more subtle way as well. My issue was my client class (the one calling a static method from an instance class) had a property of a different type but named the same as the static method. The error reported by the compiler was the same as reported here, but the issue was basically name collision.

我知道这是一个老话题,但是我花了3个小时来弄清楚我的问题是什么。我通常知道这个错误的含义,但是你也可以用一种更微妙的方式来处理它。我的问题是我的客户端类(从实例类调用静态方法)具有不同类型的属性,但命名与静态方法相同。编译器报告的错误与这里报告的错误相同,但问题基本上是名称冲突。

For anyone else getting this error and none of the above helps, try fully qualifying your instance class with the namespace name. ..() so the compiler can see the exact name you mean.

对于任何其他获得此错误的人,以上都没有帮助,请尝试使用名称空间名称完全限定您的实例类。