是否有可能创建一个具有独特属性的集合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为此做好了准备吗?
我在一个撰写文件中有多个服务,而我只需要部署一项特定服务。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) 有一个带有值的字典。它需要将每个值分配给动态实例属性。并且动态变量属性的名称应与字典的键相同。像这样的东西:
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循环,每次如何描述动态实例属性名称?谢谢你。
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 之前没有检查集合是否为空,智能感知不会发出警告?我认为它可以带来很多意想不到的结果。
情况如下:
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 ×5
c# ×5
dictionary ×2
linq ×2
autofixture ×1
casting ×1
docker ×1
docker-swarm ×1
dynamic ×1
reactjs ×1