首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码示例未编译

代码示例未编译
EN

Stack Overflow用户
提问于 2015-08-16 15:16:31
回答 1查看 174关注 0票数 0

我一直在尝试使用Microsoft提供的一些示例代码。我

  1. 下载的文件
  2. 按下文件上的取消块
  3. 解压缩文件
  4. 单击称为QuizGame示例的解决方案
  5. 在Visual 2015中打开的解决方案
  6. 该解决方案自动注册数百个错误。
  7. 我在解决方案资源管理器中打开了C#文件,查看发生了什么,每个C#文件都有大量错误。每个错误都与系统引用有多大关系。

错误CS0246无法找到类型或命名空间名称“System”(您是缺少使用指令还是程序集引用?)

警告是

警告无法解析程序集或Windows元数据文件“System.Runtime.dll”

它还显示了所有的系统导入,在它们下面有红线。听起来好像是不认识系统参考。

这是代码

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Networking.Sockets;

namespace P2PHelper
{
    public class P2PSessionHost : P2PSession, IDisposable
    {

        private Dictionary<Guid, P2PClient> ClientMap { get; set; }

        private StreamSocketListener SessionListener { get; set; }

        private Timer Timer { get; set; }

    public P2PSessionHost(P2PSessionConfigurationData config) : base(config)
    {
        this.SessionListener = new StreamSocketListener();
        this.ClientMap = new Dictionary<Guid, P2PClient>();
    }

    public void Dispose()
    {
        this.SessionListener.Dispose();
        this.SessionListener = null;
    }

    public async Task<bool> CreateP2PSession(SessionType type)
    {
        if (this.SessionListener == null) return false;
        if (type != SessionType.LocalNetwork) throw new NotSupportedException(
            "SessionType.LocalNetwork is the only SessionType supported.");

        this.SessionHost = true;
        this.SessionListener.ConnectionReceived += async (s, e) => await OnConnectionReceived(e.Socket);  
        await this.SessionListener.BindEndpointAsync(null, Settings.tcpPort);
        this.InitializeNetworkInfo();
        return await this.InitializeMulticast(null);
    }

    public bool RemoveClient(Guid clientID)
    {
        return this.ClientMap.Remove(clientID);
    }

    private bool AcceptingConnections { get; set; }
    public void StartAcceptingConnections()
    {
        AcceptingConnections = true;
        this.Timer = new Timer(async state => await SendMulticastMessage(""), null, 0, 500);   
    }

    public void StopAcceptingConnections()
    {
        AcceptingConnections = false;
        this.Timer.Dispose();
    }

    private async Task OnConnectionReceived(StreamSocket socket)
    {
        byte[] message = await RetrieveMessage(socket);
        var newClient = new P2PClient { clientTcpIP = socket.Information.RemoteAddress.ToString() };
        if (AcceptingConnections)
        { 
            if (GetGuid(newClient).ToString() == (new Guid()).ToString())
            {
                Guid newGuid = Guid.NewGuid();
                this.ClientMap.Add(newGuid, newClient);
                this.OnConnectionComplete(newGuid);
            }
        }
        this.OnMessageReceived(message, GetGuid(newClient));
    }

    private Guid GetGuid(P2PClient client)
    {
        return this.ClientMap.FirstOrDefault(
            kvp => kvp.Value.clientTcpIP == client.clientTcpIP).Key;
    }

    protected async Task SendMulticastMessage(string output)
    {
        using (var multicastOutput = await this.MulticastSocket.GetOutputStreamAsync(
            new Windows.Networking.HostName(Settings.multicastIP), 
            this.MulticastSocket.Information.LocalPort))
        {
            await multicastOutput.WriteAsync(Encoding.UTF8.GetBytes(output).AsBuffer());
        }
    }

    public async Task<bool> SendMessage(Guid clientID, object message, Type type = null)
    {
        P2PClient client;
        if (this.ClientMap.TryGetValue(clientID, out client))
        {
            return await base.SendMessage(message, client.clientTcpIP, Settings.tcpPort, type ?? typeof(object));
        }
        return false;
    }

    public async Task<bool> SendMessageToAll(object message, Type type = null)
    {
        var messageTasks = this.ClientMap.Keys.Select(guid => this.SendMessage(guid, message, type));

        // When all the tasks complete, return true if they all succeeded. 
        return (await Task.WhenAll(messageTasks)).All(value => { return value; });
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2015-08-16 17:11:03

不识别.NET引用和名称空间(如System.GenericsSystem等),这通常是由于在客户端配置文件中编译或引用使用.NET的不同版本编译的DLL造成的。转到项目设置并验证正在编译的.NET的正确版本,没有打开客户端配置文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32036654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档