约束参数,new()

本文关键字:new 参数 约束 | 更新日期: 2023-09-27 18:36:21

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

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();
    }
}

尝试了这些建议,但没有运气:

  • 将参数传递给模板化类型的 C# 泛型 new()

  • C# 中是否有带有参数约束的泛型构造函数?

  • https://social.msdn.microsoft.com/Forums/en-US/fd43d184-0503-4d4a-850c-999ca58e1444/creating-generic-t-with-new-constraint-that-has-parameters?forum=csharplanguage

  • http://www.gamedev.net/topic/577668-c-new-constraint--is-it-possible-to-add-parameters/

提前感谢!

约束参数,new()

如果你不介意使用反射,你可以像这样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});
}

演示。

不,您不能编写模板约束(例如 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> - 这将确保我们只返回 EntityId 的子类

如何使value成为私有可变字段/属性并从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));