小编And*_*ers的帖子

泛型,其中T是类实现接口

我有一个界面:

interface IProfile { ... }
Run Code Online (Sandbox Code Playgroud)

......和一个班级:

[Serializable]
class Profile : IProfile 
{ 
  private Profile()
  { ... } //private to ensure only xmlserializer creates instances
}
Run Code Online (Sandbox Code Playgroud)

...和一个经理方法:

class ProfileManager
{
  public T Load<T>(string profileName) where T : class, IProfile
  {
    using(var stream = new .......)
    {
      var ser = new XmlSerializer(typeof(T));
      return (T)ser.Deserialize(stream);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望这个方法可以像这样使用:

var profile = myManager.Load<Profile>("TestProfile"); // class implementing IProfile as T
Run Code Online (Sandbox Code Playgroud)

...并在此处抛出编译时错误:

var profile = myManager.Load<IProfile>("TestProfile"); // NO! IProfile interface entered!
Run Code Online (Sandbox Code Playgroud)

但是,所有内容都会编译,并且只会抛出运行时错误XmlSerializer. …

.net c# generics compact-framework

15
推荐指数
1
解决办法
2万
查看次数

在构建服务器上构建时,缺少包含.NetStandard项目

由于有兴趣将我们的服务器系统迁移到.Net核心,我目前正在尝试VS2017.我已经切换了几个小工具项目来定位.NetStandard 1.2(重新创建和移动文件),一切都在本地构建.

但是,当我在TeamCity 10.0.5服务器上请求构建时,构建失败并显示以下消息(项目名称已编辑):

E:\ TeamCity\buildAgent1\work\467cb2a824afdbda\Source\{PROJECT FOLDER}\{PROJECT} .csproj错误MSB4019:导入的项目"C:\ Program Files(x86)\ Microsoft Visual Studio\2017\BuildTools\MSBuild\Sdks找不到\ Microsoft.NET.Sdk\Sdk\Sdk.props".确认声明中的路径是否正确,以及该文件是否存在于磁盘上.

Microsoft.NET.Sdk文件夹确实存在于我的机器上,但不存在于构建服务器上.

我已经为Visual Studio 2017安装了构建工具,并安装了.Net Core 1.1 SDK,构建配置使用了VS2017的Visual Studio(sln)运行器.

我错过了哪些工具/安装程序?

更新: 我已经尝试使用Full Visual Studio 2017安装程序(社区),并且.NET Framework 4.5 Targeting Pack软件包添加了相关文件夹.奇怪,因为服务器上已经安装了4.5多目标包.

在设置Build代理以使用完整的VS2017社区安装后,出现了许多其他问题,例如在相关项目中找不到System命名空间.

添加dotnet还原构建步骤可以解决此问题,但是其他名称空间缺失(缺少安装),等等.

使用.NETStandard项目构建完整的框架解决方案似乎依赖于.NET Core来构建,我不明白,因为我不是针对.NET Core.

msbuild teamcity build-server .net-standard

14
推荐指数
2
解决办法
3128
查看次数

好的编码?(多个消息循环)

我需要一些正确编码的建议:

我正在开发一个使用多个串行连接的程序.每条通信线路都有一个控制器作为抽象层.在控制器和串行端口之间,插入协议以将数据包装在包中,准备传输.该协议负责发送失败,重新发送等.为确保GUI不会挂起,每个连接线(协议和串行端口)都在一个单独的线程上创建.控制器由主线程处理,因为它在GUI中具有控件.

目前,当我创建线程时,我选择在它们上创建一个消息循环(Application.Run()),所以改为轮询缓冲区并且如果没有工作则让步,我只是调用线程(BeginInvoke)并使用消息循环作为一个缓冲区.这目前工作得很好,到目前为止没有严重的问题.

我现在的问题是:这是"良好的编码",还是我应该使用while循环而不是轮询缓冲区?或者第三件事?

我想显示代码,但到目前为止它是几千行代码,所以如果你需要查看代码的任何部分,请具体说明.:)

谢谢.

c# multithreading coding-style

10
推荐指数
1
解决办法
815
查看次数

儿童任务取消,家长完成了?

当孩子被附加时,我正试图理解.net任务的行为.

我有以下测试代码:

void Test()
{
    var tokenSource = new CancellationTokenSource();
    var token = tokenSource.Token;

    Task child = null;
    var parent = Task.Factory.StartNew(() =>
    {
        child = Task.Factory.StartNew(() =>
        {
            while (!token.IsCancellationRequested)
                Thread.Sleep(100);
            token.ThrowIfCancellationRequested();
        }, token, TaskCreationOptions.AttachedToParent, TaskScheduler.Default);
    }, token);

    Thread.Sleep(500);

    Debug.WriteLine("State of parent before cancel is {0}", parent.Status);
    Debug.WriteLine("State of child before cancel is {0}", child.Status);

    tokenSource.Cancel();
    Thread.Sleep(500);

    Debug.WriteLine("State of parent is {0}", parent.Status);
    Debug.WriteLine("State of child is {0}", child.Status);
}
Run Code Online (Sandbox Code Playgroud)

结果是:

State of parent before cancel is WaitingForChildrenToComplete
State …
Run Code Online (Sandbox Code Playgroud)

c# task-parallel-library

6
推荐指数
1
解决办法
1105
查看次数

使用Docker自定义Visual Studio运行配置

我正在使用Visual Studio 2017中的Docker设置.NET Core项目.我创建了项目并将docker-compose添加到解决方案中,一切正常.

但不知何故,除了DebugRelease之外,VS不会使用任何配置构建和运行Docker .

我创建了名为Preproduction的解决方案和项目配置并选择了它,并创建了一个docker-compose.vs.preproduction.yml文件.

但是当我运行预生产配置时,项目就会像选择Release一样运行.Build Output控制台还显示以下内容:

调试

*docker-compose -f "docker-compose.yml" -f "docker-compose.override.yml" -f "docker-compose.vs.debug.yml" -p dockercompose3979710767*
Run Code Online (Sandbox Code Playgroud)

发布

*docker-compose -f "docker-compose.yml" -f "docker-compose.override.yml" -f "docker-compose.vs.release.yml" -p dockercompose3979710767*
Run Code Online (Sandbox Code Playgroud)

试生产

*docker-compose -f "docker-compose.yml" -f "docker-compose.override.yml" -f "docker-compose.vs.release.yml" -p dockercompose3979710767*
Run Code Online (Sandbox Code Playgroud)

注意第二个yml文件.它以某种方式使用了发布文件,而不是我添加的自定义配置文件.

有没有人知道如何解决这个问题,所以docker将使用我的自定义配置?

visual-studio docker .net-core docker-compose

6
推荐指数
1
解决办法
967
查看次数