【问题标题】:C# Inconsistent accessibility : parameter type is less accessible than method [duplicate]C#不一致的可访问性:参数类型比方法更难访问[重复]
【发布时间】:2015-07-08 20:54:33
【问题描述】:

我遇到了一个错误,我找不到如何修复这里的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MITM_Omar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        public void button1_Click(object sender, EventArgs e)
        {
          ClientListener Client = new ClientListener();
            Client.ClientConnected += clientconnect;
        }
        public void clientconnect(ClientListener client)
        {
            ServeurListener Serveur = new ServeurListener("213.248.126.39", 5555);
            client.associated = Serveur;
            Serveur.associated = client;
            client.onReception += forwadtoserver;
            Serveur.onReception += forwadtoclient;
            Serveur.connect(); 
        }
        public void forwadtoclient(ServeurListener sender, byte[] buffer)
        {
            sender.associated.send(buffer);
        }
        public void forwadtoserver(ClientListener sender, byte[] buffer)
        {
            sender.associated.send(buffer);
        }
    }
}

我收到错误不一致的可访问性:参数类型'MITM_OMAR.ClientListener' 比方法'MITM_OMAR.Form1.clientconnect(MITM_OMAR.ClientListener) 更难访问

这是客户端侦听器类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace MITM_Omar
{
    class ClientListener
    {
        public Socket _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        public Thread _ConnectionThread;
        public Thread _ReceptionThread;


        public delegate void onClientActionEventHandler(ClientListener client);
        public event onClientActionEventHandler ClientConnected;


        public delegate void onTravellingDataEventHandler(ClientListener sender, byte[] buffer);
        public event onTravellingDataEventHandler onReception;
        public ServeurListener associated;
       public ClientListener()
        {
            _ConnectionThread = new Thread(new ThreadStart(accept));
            _Socket.Connect("127.0.0.1", 5555);
            _ConnectionThread.Start();
            _ReceptionThread = new Thread(new ThreadStart(Reception));
            _ReceptionThread.Start();
        }
        public void accept()
        {
            while (true)
            {
                Socket socket = _Socket.Accept();
                if (ClientConnected != null)
                {
                    ClientConnected(this);
                }
            }

        }
        public void send(byte[] data)
        {
            _Socket.Send(data);


        }
        public void Reception()
        {
            // On reçoit les données tant qu'on est connecté.
            while (_Socket.Connected)
            {
                // On crée notre buffer dynamique.
                byte[] _buffer = new byte[_Socket.Available];

                // Si le buffer n'est pas vide, on le parse.
                if (_buffer.Length != 0)
                {
                    _Socket.Receive(_buffer); // Récéption des données.
                    if (onReception != null) onReception(this, _buffer); // un event qui dit qui a reçu des données et le tableau de bytes reçu

                }
            }
        }


    }
}

【问题讨论】:

  • 公开ClientListener 类。它目前是私有的。
  • 这个问题有很多重复。请搜索以查看您的问题是否已被其他人报告并解决。
  • 谢谢大家,我没注意到....

标签: c#


【解决方案1】:

公开您的课程。

public class ClientListener
...

如果你没有明确指定一个类的默认访问是内部的。 Internal 比 public 更难访问,因此您会遇到错误。

【讨论】:

  • 但是整个程序集的内部工作!但是为什么当两个类在同一个程序集中时会产生这样的错误消息呢?
  • @VijayVj - 因为,其他类的访问说明符 - Form1 是公共的。 public 意味着它可以在程序集之外访问,因此如果您在 Form 1 中使用 internal 类,它没有用,因为 internal 不能在外部访问,因此难以访问,因为它限制了 public 的权力。如果您确定您的 Form 1 类不会在外部使用,则将其设置为内部类,然后您可以在其中使用其他内部类。
猜你喜欢
  • 1970-01-01
  • 2011-09-07
  • 2014-10-31
  • 2019-05-14
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
相关资源
最近更新 更多