约束参数,new()

Constraint parameters, new()

有什么方法可以将 Parse 方法移动到抽象中 class 吗?我尝试了多种方法(底部的链接),但我仍然遇到一个或另一个障碍。

public class AnimalEntityId : EntityId<AnimalEntityId>
{
    public AnimalEntityId()
        : base()
    {
    }

    private AnimalEntityId(string value)
        : base(value)
    {
    }

    public static AnimalEntityId Parse(string value)
    {
        return new AnimalEntityId(value);
    }
}


public abstract class EntityId<TEntityId>
{
    private readonly System.Guid value;

    protected EntityId(string value)
    {
        this.value = System.Guid.Parse(value);
    }

    protected EntityId()
    {
        this.value = System.Guid.NewGuid();
    }
}

尝试了这些建议但没有成功:

提前致谢!

不,您不能编写 new(string) 之类的模板约束来代替简单的 new()。你必须利用反射来让它工作:

public abstract class EntityId<TEntityId>
    where TEntityId : EntityId<TEntityId>
{
    private readonly System.Guid value;

    protected EntityId(string value)
    {
        this.value = System.Guid.Parse(value);
    }

    protected EntityId()
    {
        this.value = System.Guid.NewGuid();
    }

    public static TEntityId Parse(string value)
    {
        return (TEntityId)Activator.CreateInstance(typeof(TEntityId), new object[] { value });
    }
}

假设您使构造函数可访问(而不是它当前是私有的)。注意约束 where TEntityId : EntityId<TEntityId> - 这将确保我们只会 return EntityId

的子类

如果你不介意使用反射,你可以像这样将Parse移动到抽象类型中:

public static TEntityId Parse(string val) {
    var constr = typeof(TEntityId).GetConstructor(
        // Since the constructor is private, you need binding flags
        BindingFlags.Instance | BindingFlags.NonPublic
    ,   null
    ,   new[]{ typeof(string) }
    ,   null);
    if (constr == null) {
        throw new InvalidOperationException("No constructor");
    }
    return (TEntityId)constr.Invoke(new object[] {val});
}

Demo.

如何使 value 成为私有可变 field/property 并实际从 Parse 方法设置它?

(为简单起见,从 EntityId 中删除了奇怪的重复通用参数)

public class SimpleAnimalEntityId : EntityId
{
    // Implicit parameterless constructor.
}

public class ParametrizedAnimalEntityId : EntityId
{
    // Parametrized constructor only.
    public ParametrizedAnimalEntityId(int ignored)
    {
    }
}

public abstract class EntityId
{
    // Simple scenario: derived type has a parameterless constructor.
    public static TEntity Parse<TEntity>(string value)
        where TEntity : EntityId, new()
    {
        Guid id = Guid.Parse(value);

        return new TEntity { value = id };
    }

    // Advanced scenario: derived type constructor needs parameters injected.
    public static TEntity Parse<TEntity>(string value, Func<TEntity> constructor)
        where TEntity : EntityId
    {
        Guid id = Guid.Parse(value);
        TEntity entity = constructor();

        entity.value = id;

        return entity;
    }

    private Guid value;

    protected EntityId()
    {
        value = Guid.NewGuid();
    }
}

现在您可以从 Parse 方法处理任何构造函数:

string id = Guid.NewGuid().ToString();
SimpleAnimalEntityId simple = EntityId.Parse<SimpleAnimalEntityId>(id);
ParametrizedAnimalEntityId parametrized = EntityId.Parse(id, () => new ParametrizedAnimalEntityId(42));