相关疑难解决方法(0)

如何注册通用对象工厂?

我有以下两个类:

public class KeyedEntity<TEntity>
{
    internal KeyedEntity() { }

    public Identifier Key { get; set; }
    public TEntity Entity { get; set; }
}

public static class KeyedEntity
{
    public static KeyedEntity<TEntity> Create<TEntity>(Identifier key, TEntity entity)
    {
        return new KeyedEntity<TEntity>
        {
            Key = key,
            Entity = entity,
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

构造函数internal和第二个类存在的原因是我想强制执行更高度可维护的KeyedEntity.Create(x, y)语法而不是new KeyedEntity<T>{ Key = x, Entity = y }. (请注意,类型是用前一种语法推断出来的。)

我想告诉 AutoFixture 如何创建KeyedEntity. 但是,该Register方法似乎只允许注册单个类型而不是开放的泛型类型。

如何注册KeyedEntity.Create<TEntity>为 的创建函数KeyedEntity<TEntity> …

.net generics autofixture open-generics

5
推荐指数
1
解决办法
719
查看次数

标签 统计

.net ×1

autofixture ×1

generics ×1

open-generics ×1