如何创建另一个类中存在的类的新实例?

时间:2018-11-09 22:31:32

标签: c# class

我想创建一个存在于另一个类中的类的新实例,并访问您的成员。

基于下面的代码,我想创建在服务器桌面应用程序中连接的客户端的列表。

然后我尝试了以下代码,但说不是“ Command”类是必需的。

我该如何解决?

Client.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class Client
{
    public class Command
    {
        public Socket sock;
        public Guid ID;
        public string RemoteAddress;
        public byte[] buffer = new byte[8192];

        public Command(Socket sock)
        {
            this.sock = sock;
            ID = Guid.NewGuid();
            RemoteAddress = sock.RemoteEndPoint.ToString();
        }

        public void Send(string data)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
            {
                sock.EndSend(ar);
            }), buffer);
        }
    }

    public class Screenshot
    {
        public Socket sock;
        public Guid ID;
        public string RemoteAddress;
        public byte[] buffer = new byte[8192];

        public Screenshot(Socket sock)
        {
            this.sock = sock;
            ID = Guid.NewGuid();
            RemoteAddress = sock.RemoteEndPoint.ToString();
        }

        public void Send(string data)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
            {
                sock.EndSend(ar);
            }), buffer);
        }
    }
}

在其他课程中,我尝试过:

class Listener
{

Socket s;
public List<Command> clients; // not accessible "Command"

public Listener()
{
    clients = new List<Client>();
    s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}

public void BeginListen(int port)
{
    s.Bind(new IPEndPoint(IPAddress.Any, port));
    s.Listen(100);
    s.BeginAccept(new AsyncCallback(AcceptCallback), s);
}

void AcceptCallback(IAsyncResult ar)
{
    Socket handler = (Socket)ar.AsyncState;
    Socket sock = handler.EndAccept(ar);
    Command client = new Command(sock); // not accessible "Command"
    clients.Add(client);

    sock.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), client);
    handler.BeginAccept(new AsyncCallback(AcceptCallback), handler);
}

1 个答案:

答案 0 :(得分:0)

您不能使用List<Command>来指定新的实例类型为List<Client>,因为ClientCommand类不同,它是类的内部。

public List<Command> clients; // not accessible "Command"

public Listener()
{
    clients = new List<Client>();
    s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}

尝试使用Client.Command代替CommandClient

public List<Client.Command> clients; 

public Listener()
{
    clients = new List<Client.Command>();
    s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}