Kie*_*gas 3 c# foreach concurrentdictionary .net-4.5 visual-studio-2012
我正在学习构建聊天客户端和服务器的教程,现在我遇到了以下错误:
Inconsistent accessibility: field type 'System.Collections.Concurrent.ConcurrentDictionary<string,ChattingServer.ConnectedClient>' is less accessible than field 'ChattingServer.ChattingService._connectedClients' c:\Users\KOEMXE\Documents\Visual Studio 2012\Projects\ChatApplication\ChattingServer\ChattingService.cs 17 62 ChattingServer
Run Code Online (Sandbox Code Playgroud)
这是相关问题所在的类文件 (ChattingService.cs):
using ChattingInterfaces;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ChattingServer
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public class ChattingService : IChattingService
{
//private ConnectedClient _connectedClients;
public ConcurrentDictionary<string, ConnectedClient> _connectedClients = new ConcurrentDictionary<string, ConnectedClient>();
public int Login(string userName)
{
//is anyone else logged in with my name?
foreach (var client in _connectedClients)
{
if(client.Key.ToLower() == userName.ToLower())
{
//if yes
return 1;
}
}
var establishedUserConnection = OperationContext.Current.GetCallbackChannel<IClient>();
ConnectedClient newClient = new ConnectedClient();
newClient.connection = establishedUserConnection;
newClient.UserName = userName;
_connectedClients.TryAdd(userName, newClient);
return 0;
}
public void SendMessageToALL(string message, string userName)
{
foreach (var client in _connectedClients)
{
if (client.Key.ToLower() != userName.ToLower())
{
client.Value.connection.GetMessage(message, userName);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我是否需要在其他地方的 ConcurrentDictionary 对象中声明类型?有什么问题?谢谢!
ChattingService是公开的,其_connectedClients成员是公开的。但是_connectedClients涉及的类型ChattingServer.ConnectedClient不是公开的。
想象一下,您ChattingServer要从另一个项目中引用您的程序集,然后编写如下代码:
using ChattingServer;
...
ConcurrentDictionary<string, ConnectedClient> dict = myChattingService._connectedClients;
Run Code Online (Sandbox Code Playgroud)
您的ChattingService类型及其_connectedClients字段的可访问性允许您访问该字段,但该类型ConnectedClient不是公开的,因此您可以根据您在类型和成员上放置的可访问性修饰符访问其类型应该隐藏的字段该ChattingServer装配。这就是您收到构建错误的原因:您的类型和ChattingServer程序集中成员的可访问性修饰符相互矛盾。
例如,如果您ChattingServer.ConnectedClient公开或_connectedClients私有,这将解决问题。
| 归档时间: |
|
| 查看次数: |
351 次 |
| 最近记录: |