小编Ole*_*egI的帖子

AutoFixture 构建具有独特属性的集合

是否有可能创建一个具有独特属性的集合AutoFixture?例如,我想创建一个集合:

public class Foo {
 public int Id {get; set;}
 public string Name {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

具有独特的Id. 它看起来像这样:

var fixture = new Fixture();

fixture
 .Build<Foo>()
 .WithUnique((foo) => foo.Id)
 .CreateMany(20);
Run Code Online (Sandbox Code Playgroud)

我知道可以通过定制来做到这一点,但我认为这是很常见的情况,所以可能已经AutoFixture为此做好了准备吗?

.net c# autofixture

8
推荐指数
1
解决办法
7348
查看次数

Docker Swarm 堆栈部署特定服务

我在一个撰写文件中有多个服务,而我只需要部署一项特定服务。docker swarm 支持这个吗?docker-compose 文件示例:

version: '3.7'
services:
  bar:
    image: bar:latest
    deploy:
        resources:
          limits:
            memory: 256M

  foo:
    image: foo:latest
    deploy:
      resources:
        limits:
          memory: 256M  
Run Code Online (Sandbox Code Playgroud)

因此,部署到堆栈mystack两个服务的命令是:

docker stack deploy -c docker/docker-compose.yml mystack
Run Code Online (Sandbox Code Playgroud)

我只想部署Foo服务。期望命令类似于:

docker stack deploy -c docker/docker-compose.yml foo mystack
Run Code Online (Sandbox Code Playgroud)

docker docker-compose docker-swarm

7
推荐指数
1
解决办法
5387
查看次数

Visual Studio 无法识别扩展名为 .js 的 React 文件

在此输入图像描述我面临的问题是 Visual Studio 2017 无法识别带有 js(不是 jsx)扩展名的 React 文件。它将所有内容都突出显示为语法错误,这使得代码完全不可读且无法使用。它迫使我在 VS Code 和 VS 两种不同的环境中工作(因为 VS Code 没有像 Resharper 这样的东西,而且基本上在我的主观意见中 VS 更适合与 .Net 一起工作)。有什么可能的解决方法吗?可能是 VS 与 React 一起使用的扩展?花了一些时间但找不到任何可以解决这个问题的东西

.net c# visual-studio reactjs

5
推荐指数
0
解决办法
239
查看次数

将值从字典分配给动态对象 C#

有一个带有值的字典。它需要将每个值分配给动态实例属性。并且动态变量属性的名称应与字典的键相同。像这样的东西:

dynamic response = new ExpandoObject();
var dict = new Dictionary<string, string>();
string s;
dict.TryGetValue("Name", out s); // imagine dictionary contains some data
response.Name = s;
dict.TryGetValue("Street", out s);
response.Street = s;
....
Run Code Online (Sandbox Code Playgroud)

我确信有一些更有效的方法然后只是迭代每个值。如果要创建for循环,每次如何描述动态实例属性名称?谢谢你。

.net c# dictionary dynamic

3
推荐指数
1
解决办法
3724
查看次数

如果集合为空,LINQ Enumerable.All 始终返回 True

    class Program
    {
        static void Main()
        {
            var list = new List<Foo>();
            var a = list.All(l => l.BoolBar == true);//true
            var s = list.All(l => l.Bar.Contains("magicstring"));//true
        }
    }
    public class Foo
    {
        public bool BoolBar{ get; set; }
        public string Bar{ get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

基于这个片段,我想知道为什么 Linq 框架创建者选择使用这个解决方案来处理空集合?另外,由于 Visual Studio 和 Linq 都是 MS 产品,为什么如果用户在执行 .All 之前没有检查集合是否为空,智能感知不会发出警告?我认为它可以带来很多意想不到的结果。

.net c# linq

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

从List <T>属性中检索值并强制转换为Dictionary <string,string> C#

情况如下:

public class Foo
{
  public string Name {get;set;}
  public List<Bar> barITems{get; set;}
}
public class Bar
{
  public string Name{get; set;}
  public string Address{get; set;}
}
Run Code Online (Sandbox Code Playgroud)

而我需要创建Dictionary其中的键--Bar类的属性名和值--Bar类的相关值.我在这里尝试了:

    Type a = typeof(Foo);
    PropertyInfo[] properties = a.GetProperties();           
    foreach (var property in properties)
    {                       
        if(property.Name == "barItems")
        {
          var barProperty = property.GetValue(Foo);
          var itemList= barProperty as IEnumerable;
          var barDictionary = new Dictionary<string,string>();
          barDictionary = itemList;  //how to cast it ?           
          continue;
        }
        fooDictionary.Add(property.Name, property.GetValue(Foo).ToString());
    }
Run Code Online (Sandbox Code Playgroud)

.net c# linq dictionary casting

0
推荐指数
1
解决办法
82
查看次数