Autofixture: 并发字典

创建于 2020-04-24  ·  6评论  ·  资料来源: AutoFixture/AutoFixture

你好,
当我使用 autofixture 生成Dictionary<T, U>时,默认情况下会在其中填充值,但使用ConcurrentDictionary<T, U>时不会。

这是设计使然,我应该使用样本构建器自己更改该行为,还是缺少功能或错误?

最有用的评论

你好,
我无法说出这背后的原因,但它查看了支持的集合是硬编码的源代码。
所以它不是“类型实现 IDictionary”,而是“类型 Dictionary<,>”。 因此 ConcurrentDictionary 不起作用。
你不需要创建你自己的标本建造者,你可以使用已经存在的东西:

      var fixture = new Fixture();

      var query = new ModestConstructorQuery();
      var methodInvoker = new MethodInvoker(query);
      var dictionaryFiller = new DictionaryFiller();
      var postProcessor = new Postprocessor(methodInvoker, dictionaryFiller);
      var exactTypeSpecification = new ExactTypeSpecification(typeof(ConcurrentDictionary<,>));
      var specimenBuilder = new FilteringSpecimenBuilder(postProcessor, exactTypeSpecification);

      fixture.Customizations.Add(specimenBuilder);

所有6条评论

如果其他人遇到同样的问题,我为此编写了一个样本构建器:

public class ConcurrentDictionaryGenerator : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        Type type = request as Type;
        if (type == null)
        {
            return new NoSpecimen();
        }

        Type[] typeArguments = type.GetTypeInfo().GetGenericArguments();
        if (typeArguments.Length != 2 || typeof(ConcurrentDictionary<,>).MakeGenericType(typeArguments) != type)
        {
            return new NoSpecimen();
        }

        Type nonConcurrentDictionaryType = typeof(Dictionary<,>).MakeGenericType(typeArguments);
        IDictionary nonConcurrentDictionary = (IDictionary) context.Resolve(nonConcurrentDictionaryType);

        IDictionary concurrentDictionary = (IDictionary) Activator.CreateInstance(type);

        foreach (object key in nonConcurrentDictionary.Keys)
        {
            object value = nonConcurrentDictionary[key];
            concurrentDictionary.Add(key, value);
        }

        return concurrentDictionary;
    }
}

你好,
我无法说出这背后的原因,但它查看了支持的集合是硬编码的源代码。
所以它不是“类型实现 IDictionary”,而是“类型 Dictionary<,>”。 因此 ConcurrentDictionary 不起作用。
你不需要创建你自己的标本建造者,你可以使用已经存在的东西:

      var fixture = new Fixture();

      var query = new ModestConstructorQuery();
      var methodInvoker = new MethodInvoker(query);
      var dictionaryFiller = new DictionaryFiller();
      var postProcessor = new Postprocessor(methodInvoker, dictionaryFiller);
      var exactTypeSpecification = new ExactTypeSpecification(typeof(ConcurrentDictionary<,>));
      var specimenBuilder = new FilteringSpecimenBuilder(postProcessor, exactTypeSpecification);

      fixture.Customizations.Add(specimenBuilder);

我对这个库的内部了解得越多,我就越喜欢它

有趣的是,它目前只在Dictionary<,>上,感谢内置的方法!

我想有人剩下的唯一问题是:这会是一个很好的功能吗? 也许不是专门针对ConcurrentDictionary<,> ,而是针对所有IDictionary<,>实现?

只是我没有传播错误的信息,它不仅是 Dictionary<,>。 我只是想指出,类型列表可以直接在 Fixture.cs 中找到。 例如,它也是 SortedDictionary<,>,但正如您已经知道的,它不是 ConcurrentDictionary<,>。
无论如何,我会说未来的实现应该检查接口。

实际上,硬编码的不是支持,而是接口被中继到某种具体类型的事实。 像 IDictionary 和 IReadOnlyDictionary 被中继到 Dictionary,等等。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Eldar1205 picture Eldar1205  ·  5评论

ploeh picture ploeh  ·  7评论

ploeh picture ploeh  ·  3评论

ecampidoglio picture ecampidoglio  ·  7评论

Ephasme picture Ephasme  ·  3评论