访问c#中内部类的成员

时间:2022-09-02 09:40:17

I tried finding way of this seemingly easy task but with no success. Sorry I'm new to oop and c#. Pardon if it's trivial.
I have to access verb in following class.

我试图找到这个看似简单的任务的方法,但没有成功。对不起,我是oop和c#的新手。请原谅,如果它是微不足道的。我必须在下面的课程中访问动词。

public class testclass
{
 public class selector
  {
    public static string verb { get; set; }
  }
  //some other classes here
 }

One api class deserliazes and return an object of testclass.

一个api类deserliazes并返回testclass的对象。

var res = apicall(); //retruns testclass object

I want to access verb and retrieve it's value. How can this be done?

我想访问动词并检索它的值。如何才能做到这一点?

2 个解决方案

#1


2  

Since that is a static property, you access it as:

由于这是一个静态属性,因此您可以访问它:

string str = testclass.selector.verb;

Note that the access has nothing to the with the instance returned by apicall().

请注意,访问与apicall()返回的实例无关。


EDIT: if verb were not a static member, then you'd need an instance of selector defined somewhere to access that member. For example:

编辑:如果动词不是静态成员,那么你需要在某处定义一个选择器实例来访问该成员。例如:

public class TestClass
{
     public class Selector
     {
          public string Verb{ get; set; }
     }

     public Selector SomeSelector {get; set;}  
 }

Now you have a member of type selector, which you can access like:

现在你有了一个类型选择器的成员,你可以访问它:

 var res = apicall();
 var str = res.SomeSelector;

#2


1  

You can access verb with:

您可以使用以下命令访问动词:

testclass.selector.verb

because it is a static property which means it is defined to this specific type and not an object.

因为它是一个静态属性,这意味着它被定义为此特定类型而不是对象。

BUT, if you want to be point to verb with the res object, you will need to define the type for the res variable, otherwise the compiler will not let you compile this code. example:

但是,如果您想要使用res对象指向动词,则需要为res变量定义类型,否则编译器将不允许您编译此代码。例:

testclass res = apicall();

#1


2  

Since that is a static property, you access it as:

由于这是一个静态属性,因此您可以访问它:

string str = testclass.selector.verb;

Note that the access has nothing to the with the instance returned by apicall().

请注意,访问与apicall()返回的实例无关。


EDIT: if verb were not a static member, then you'd need an instance of selector defined somewhere to access that member. For example:

编辑:如果动词不是静态成员,那么你需要在某处定义一个选择器实例来访问该成员。例如:

public class TestClass
{
     public class Selector
     {
          public string Verb{ get; set; }
     }

     public Selector SomeSelector {get; set;}  
 }

Now you have a member of type selector, which you can access like:

现在你有了一个类型选择器的成员,你可以访问它:

 var res = apicall();
 var str = res.SomeSelector;

#2


1  

You can access verb with:

您可以使用以下命令访问动词:

testclass.selector.verb

because it is a static property which means it is defined to this specific type and not an object.

因为它是一个静态属性,这意味着它被定义为此特定类型而不是对象。

BUT, if you want to be point to verb with the res object, you will need to define the type for the res variable, otherwise the compiler will not let you compile this code. example:

但是,如果您想要使用res对象指向动词,则需要为res变量定义类型,否则编译器将不允许您编译此代码。例:

testclass res = apicall();