-2

我想在一个类中定义一个类。

例子:

private class newClient
{
    public int ID { get; set; }
    public string Name{ get; set; }        

    private class ClientExtraData
    {
       public string ExtraField1 { get; set; }
       public string ExtraField2 { get; set; }
    }
}

现在我想以这种方式访问​​ ExtraField1:

newClient nclnt= new newClient();
string s=nclnt.ClientExtraData.ExtraField1;

这可以做到吗?

4

6 回答 6

3

要实现以下目标,

newClient nclnt= new newClient();
string s=nclnt.ClientExtraData.ExtraField1;

您需要做的就是公开这样的属性

private class NewClient
{
        public class NewClient() 
        {
            this.ClientExtraData = new ClientData();
        }
        public int ID { get; set; }
        public string Name{ get; set; }
        public ClientData ClientExtraData {get;set;}
}

ClientData你的程序集中的一个类在哪里。

于 2012-10-30T09:04:59.643 回答
1

No it can't be done like that. The class is not a method, property or anything which is instanziated by the class.

It is also very bad design :-)

You could however say:

var myclass = new ClassOne.ClassTwo()

于 2012-10-30T09:04:21.043 回答
0
  1. 使内部类公开,以便可以访问它。
  2. 将您需要访问的属性设为静态(或创建实例以访问该字段)

类 newClient { public int ID { get; 放; } 公共字符串名称 { 获取;放; }

    public class ClientExtraData
    {
        public static string ExtraField1 { get { return "10"; }  }
        public string ExtraField2 { get; set; }
    }
}

访问代码:

newClient nclnt = new newClient(); 字符串 s = newClient.ClientExtraData.ExtraField1;

于 2012-10-30T09:14:23.973 回答
0

用你的代码准确地归档这个

newClient nclnt= new newClient();
string s=nclnt.ClientExtraData.ExtraField1;

newClient班级必须是,班级必须是public 并且ClientExtraDatapublicstatic

但更好的方法是明确地创建类,其中 newClient 包含一个 Get-Property 到 ClientExtraData 类的实例。

查看访问修饰符(C# 编程指南)

于 2012-10-30T09:09:44.020 回答
0

I usually do this kind of stuff like this:

//the two classes can live in the same code file, if you want.
private class ClientExtraDataClass
{
    public string ExtraField1 { get; set; }
   public string ExtraField2 { get; set; }
}
private class newClient
{
    public int ID { get; set; }
    public string Name{ get; set; }

    //you may want to make it a readonly property too, 
    // it depends on your needs.
    public ClientExtraDataClass ClientExtraData = new ClientExtraDataClass();
}
于 2012-10-30T09:03:50.200 回答
0

不,您不能以这种方式访问​​内部类,nclnt.ClientExtraData.ExtraField1;因为如果要访问其属性,则必须拥有该对象的实例。然后你不能使用private访问修饰符,但你必须以internal这种方式使用:

class newClient
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ClientExtraData clientExtraData{ get; set; }

    public newClient()
    {
        clientExtraData = new ClientExtraData();
    }

    internal class ClientExtraData
    {
        public string ExtraField1 { get; set; }
        public string ExtraField2 { get; set; }
    }
}

或者,如果您想以这种方式使其在外部可访问,则可以ClientExtraData public代替:internalnewClient

newClient.ClientExtraData clientExtraData = new newClient.ClientExtraData();

那么你可以这样做:

newClient nclnt= new newClient();
string s = nclnt.clientExtraData.ExtraField1;
于 2012-10-30T09:05:34.693 回答