[英]C# Namespaces inside classes?
Not sure how to explain this, so I'll try to give as much detail as possible. 不知道如何解释这一点,所以我将尝试提供尽可能多的细节。 I'm making a Net Library and I need to give a section to my NetClient class, such as Headers in this example:
我正在制作一个网络库,需要为我的NetClient类提供一个部分,例如本例中的Headers :
NetClient netClient = new NetClient("host", port);
netClient.Headers.Add("Name", "Value");
I would think this would work, but it doesn't (can't see the Headers class at all in an instance of NetClient class): 我认为这可以工作,但不起作用(在NetClient类的实例中根本看不到Headers类):
namespace NetLib
{
class NetClient
{
public string Host { get; }
public int Port { get; }
public NetClient(string host, int port)
{
this.Host = host;
this.Port = port;
}
class Headers
{
class Header
{
public string Name { get; }
public string Value { get; }
internal Header(string name, string value)
{
this.Name = name;
this.Value = value;
}
}
I solved my problem with the help of submitted answers, this is what my code looks like now: 我在提交的答案的帮助下解决了我的问题,这就是我的代码现在的样子:
public sealed class NetClient
{
public string Host { get; set; }
public int Port { get; set; }
public Headers Headers { get; private set; }
public NetClient(string host, int port)
{
this.Headers = new Headers();
this.Host = host;
this.Port = port;
}
}
public sealed class Headers
{
public class Header
{
public string Name { get; }
public string Value { get; }
internal Header(string name, string value)
{
this.Name = name;
this.Value = value;
}
}
Putting a class inside another class doesn't make it an instance member of the class (or even a static member), it only affects the naming and scope of the class. 将一个类放到另一个类中并不会使其成为该类的实例成员(甚至是静态成员),而只会影响该类的命名和范围。
To get an instance member you need an actual member of the class, for example a property that is a list of header items: 要获得实例成员,您需要一个类的实际成员,例如,作为标头项目列表的属性:
namespace NetLib {
class Header {
public string Name { get; }
public string Value { get; }
public Header(string name, string value) {
this.Name = name;
this.Value = value;
}
}
class NetClient {
public string Host { get; private set; }
public int Port { get; private set; }
public List<Header> Headers { get; private set; }
public NetClient(string host, int port) {
this.Host = host;
this.Port = port;
this.Headers = new List<Header>();
}
}
}
Usage: 用法:
NetClient netClient = new NetClient("host", port);
netClient.Headers.Add(new Header("Name", "Value"));
You could put the Header
class inside the NetClient
class, but then you need to use new NetClient.Header(...)
instead of new Header(...)
. 您可以将
Header
类放在NetClient
类中,但随后需要使用new NetClient.Header(...)
而不是new Header(...)
。
Try making it public, as by default classes are "internal" and members/variables inside are private. 尝试将其公开,因为默认情况下,类是“内部”的,而内部的成员/变量是私有的。
So the basic idea is to have internal instance creation so that the class will be initialized and the object will be created at run-time. 因此,基本思想是创建内部实例,以便初始化类并在运行时创建对象。 Please do not copy paste as is as I have not used VS to rectify.
请不要按原样复制粘贴,因为我没有使用VS进行纠正。
I think it should be more like : 我认为应该更像是:
NetClient netClient = new NetClient("host", port); NetClient netClient =新的NetClient(“主机”,端口); netClient.Headers=netclient.CreateObjHeaders("Name", "Value");
netClient.Headers = netclient.CreateObjHeaders(“ Name”,“ Value”);
namespace NetLib { class NetClient { public string Host { get; 命名空间NetLib {类NetClient {公共字符串Host {get; } public int Port { get;
} public int Port {get; }
}
public NetClient(string host, int port)
{
this.Host = host;
this.Port = port;
}
public Headers CreateObjHeaders(string Name, string Value)
{
Headers objHeaders=new Headers("Name", "Value");
return objHeaders;
}
public class Headers
{
public Headers(string Name, string Value)
{
Header objHeader=new Header("Name", "Value");
}
public class Header
{
public string Name { get; }
public string Value { get; }
internal Header(string name, string value)
{
this.Name = name;
this.Value = value;
}
}
Would the following code provide you with what you need? 以下代码是否可以为您提供所需的信息?
public sealed class NetClient
{
public string Host { get; set; }
public int Port { get; set; }
public Headers Headers { get; private set; }
public NetClient(string host, int port)
{
Host = host;
Port = port;
Headers = new Headers();
}
}
public sealed class Headers : Dictionary<String, String>
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.