C# 无法通过嵌套类型访问外部类型的非静态成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16320935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:44:42  来源:igfitidea点击:

Cannot access a non-static member of outer type via nested type

c#class

提问by Kamil

I have error

我有错误

Cannot access a non-static member of outer type 'Project.Neuro' via nested type 'Project.Neuro.Net'

无法通过嵌套类型“Project.Neuro.Net”访问外部类型“Project.Neuro”的非静态成员

with code like this (simplified):

使用这样的代码(简化):

class Neuro
{
    public class Net
    {
        public void SomeMethod()
        {
            int x = OtherMethod(); // error is here
        }
    }

    public int OtherMethod() // its outside Neuro.Net class
    {
        return 123;  
    }
}

I can move problematic method to Neuro.Net class, but I need this method outside.

我可以将有问题的方法移至 Neuro.Net 类,但我需要外部使用此方法。

Im kind of objective programming newbie.

我是一种客观的编程新手。

Thanks in advance.

提前致谢。

采纳答案by D Stanley

The problem is that nestedclasses are not derivedclasses, so the methods in the outer class are not inherited.

问题是嵌套类不是派生类,因此外部类中的方法不是继承的

Some options are

一些选项是

  1. Make the method static:

    class Neuro
    {
        public class Net
        {
            public void SomeMethod()
            {
                int x = Neuro.OtherMethod(); 
            }
        }
    
        public static int OtherMethod() 
        {
            return 123;  
        }
    }
    
  2. Use inheritance instead of nesting classes:

    public class Neuro  // Neuro has to be public in order to have a public class inherit from it.
    {
        public static int OtherMethod() 
        {
            return 123;  
        }
    }
    
    public class Net : Neuro
    {
        public void SomeMethod()
        {
            int x = OtherMethod(); 
        }
    }
    
  3. Create an instance of Neuro:

    class Neuro
    {
        public class Net
        {
            public void SomeMethod()
            {
                Neuro n = new Neuro();
                int x = n.OtherMethod(); 
            }
        }
    
        public int OtherMethod() 
        {
            return 123;  
        }
    }
    
  1. 制作方法static

    class Neuro
    {
        public class Net
        {
            public void SomeMethod()
            {
                int x = Neuro.OtherMethod(); 
            }
        }
    
        public static int OtherMethod() 
        {
            return 123;  
        }
    }
    
  2. 使用继承而不是嵌套类:

    public class Neuro  // Neuro has to be public in order to have a public class inherit from it.
    {
        public static int OtherMethod() 
        {
            return 123;  
        }
    }
    
    public class Net : Neuro
    {
        public void SomeMethod()
        {
            int x = OtherMethod(); 
        }
    }
    
  3. 创建一个实例Neuro

    class Neuro
    {
        public class Net
        {
            public void SomeMethod()
            {
                Neuro n = new Neuro();
                int x = n.OtherMethod(); 
            }
        }
    
        public int OtherMethod() 
        {
            return 123;  
        }
    }
    

回答by vlad

you need to instantiate an object of type Neurosomewhere in your code and call OtherMethodon it, since OtherMethodis not a static method. Whether you create this object inside of SomeMethod, or pass it as an argument to it is up to you. Something like:

您需要Neuro在代码中的某处实例化一个类型的对象并调用OtherMethod它,因为OtherMethod它不是静态方法。是在 内部创建此对象SomeMethod,还是将其作为参数传递给它取决于您。就像是:

// somewhere in the code
var neuroObject = new Neuro();

// inside SomeMethod()
int x = neuroObject.OtherMethod();

alternatively, you can make OtherMethodstatic, which will allow you to call it from SomeMethodas you currently are.

或者,您可以使OtherMethod静态,这将允许您SomeMethod按当前状态调用它。

回答by T.S.

Even though class is nested within another class, it is still not obvious which instance of outer class talks to which instance of inner class. I could create an instance of inner class and pass it to the another instance of outer class. Therefore, you need specific instance to call this OtherMethod().

即使类嵌套在另一个类中,仍然不清楚外部类的哪个实例与内部类的哪个实例对话。我可以创建内部类的实例并将其传递给外部类的另一个实例。因此,您需要特定的实例来调用 this OtherMethod()

You can pass the instance on creation:

您可以在创建时传递实例:

class Neuro
{
    public class Net
    {
        private Neuro _parent;
        public Net(Neuro parent)
        {
         _parent = parent;
        }
        public void SomeMethod()
        {
            _parent.OtherMethod(); 
        }
    }

    public int OtherMethod() 
    {
        return 123;  
    }
}

回答by Tar?k ?zgün Güner

I think making an instance of outer class in inner class is not a good option because you may executing business logic on outer class constructor. Making static methods or properties is better option. If you insist making an instance of outer class than you should add another parameter to outer class contructor that not to execute business logic.

我认为在内部类中创建外部类的实例不是一个好的选择,因为您可能会在外部类构造函数上执行业务逻辑。制作静态方法或属性是更好的选择。如果您坚持创建外部类的实例,那么您应该向外部类构造函数添加另一个不执行业务逻辑的参数。