简体   繁体   English

在C#中访问子类的属性

[英]Accessing a property of a subclass in C#

I'm banging my head against a wall for what seems like a really simple thing, but there's clearly something small that I'm missing to get this to work. 我的头撞在墙上,看起来很简单,但是显然缺少一些小东西才能使它起作用。

I have the following code 我有以下代码

public class PagedListArgs
{
     public class DataViewParts
     {
         private string _type;
         public string Type
         {
             get { return _type; }
             set { _type = value; }
         }

         private string _filter;
         public string Filter
         {
             get { return _filter; }
             set { _filter = value; }
         }  
     }
}

Later on I'm using these as follows: 稍后,我将这些使用如下:

PagedListArgs pagedListArgs = new PagedListArgs(...);
pagedListArgs.DataViewParts.Type = pagedListArgs.DataViewParts.Type.ToLower();

I'm getting the following exception 我收到以下异常

'DataViewParts': cannot reference a type through an expression; try 'PagedListArgs.DataViewParts' instead

What is it that I'm doing wrong? 我做错了什么事?

EDIT 编辑

Okay, I did a poor job of explaining the issue I'm having, and that's on me. 好的,我在解释我遇到的问题上做得很差,这就是我的意思。 Basically the issue was that the constructor for DataViewParts was being fired every time you tried accessing it. 基本上,问题是每次您尝试访问DataViewParts的构造函数时都会被触发。 Moving the constructor logic into PagedListArgs fixed the issue I was having. 将构造函数逻辑移到PagedListArgs中可以解决我遇到的问题。

DataViewParts is a class, not an instance of a DataViewParts, but you're trying to access it that way. DataViewParts是一个类,而不是DataViewParts的实例,但是您正在尝试以这种方式访问​​它。 Are you trying to do something like this? 您是否正在尝试做这样的事情?

public class PagedListArgs
{
     public PagedListArgs(){
         MyParts = new DataViewParts();
     }

     public DataViewParts MyParts { get; set; }

     public class DataViewParts
     {
         private string _type;
         public string Type
         {
             get { return _type; }
             set { _type = value; }
         }

         private string _filter;
         public string Filter
         {
             get { return _filter; }
             set { _filter = value; }
         }  
     }
}

You have only created the class for DataViewParts but no instance of it. 您只为DataViewParts创建了类,但没有创建它的实例。

Either add an instance of your subclass to the main class or make the properties static. 将您的子类的一个实例添加到主类或使属性静态。 this way they can be accessed by the type name itself 这样,就可以通过类型名称本身来访问它们

For instance: 例如:

public class PagedListArgs
{
     public DataViewParts dataViewParts;

     public class DataViewParts
     {
         private string _type;
         public string Type
         {
             get { return _type; }
             set { _type = value; }
         }

         private string _filter;
         public string Filter
         {
             get { return _filter; }
             set { _filter = value; }
         }  
     }
}

Then you can access the instance by caliing 然后,您可以通过校准来访问实例

dataViewParts = new DataViewParty();
dataViewParts.DoStuff();

You should create an instance of nested class to get it's property. 您应该创建一个嵌套类的实例来获取它的属性。

public class PagedListArgs
{
     public DataViewParts viewParts;
     public class DataViewParts
     {
         private string _type;
         public string Type
         {
             get { return _type; }
             set { _type = value; }
         }

         private string _filter;
         public string Filter
         {
             get { return _filter; }
             set { _filter = value; }
         }  
     }
}

So the second part of code becomes 所以代码的第二部分变成

PagedListArgs pagedListArgs = new PagedListArgs(...);
pagedListArgs.DataViewParts.Type = pagedListArgs.viewParts.Type.ToLower();

When you create instance of PagedListArgs you do not create a instance of DataViewParts . 创建PagedListArgs实例时,不会创建DataViewParts实例。

Do you mean, for example, setting a property in the constructor: 您的意思是,例如,在构造函数中设置属性:

public class PagedListArgs
{
     public DataViewParts DataViewParts { get; set; }

     public PagedListArgs(){
         DataViewParts = new DataViewParts();
     }

     public class DataViewParts
     {
         private string _type;
         public string Type
         {
             get { return _type; }
             set { _type = value; }
         }

         private string _filter;
         public string Filter
         {
             get { return _filter; }
             set { _filter = value; }
         }  
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM