Member '' cannot be accessed with an instance reference

前端 未结 10 1694
我在风中等你
我在风中等你 2020-11-22 10:39

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

namespace MyDataLayer
{
    namespace Section1
    {
        public class MyClass
        {
            publ         


        
相关标签:
10条回答
  • 2020-11-22 11:06

    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.)

    • Think about thread safety when using static entities.

    Some details on static in MSDN:

    • Static Classes in C#
    • Static Constructors in C#
    0 讨论(0)
  • 2020-11-22 11:07

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

    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:

    • Fully qualify static class name with according namepace, which result on Foo.Bar.Jump() declaration. You will also need to remove Using Bar; statement
    • Rename namespace Bar by a diffente name.

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

    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.

    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:

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

    Hope it helps

    0 讨论(0)
  • 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) {

    0 讨论(0)
  • 2020-11-22 11:10

    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:

    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;
        }
    }       
    
    0 讨论(0)
  • 2020-11-22 11:12

    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.

    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.

    0 讨论(0)
  • 2020-11-22 11:13

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

    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.]

    0 讨论(0)
提交回复
热议问题