有什么办法可以将的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();
}
}
尝试了这些建议,没有运气:
Is there a generic constructor with parameter constraint in C#?
- http://www.gamedev.net/topic/577668-c-new-constraint--is-it-possible-to-add-parameters/
在此先感谢!
澄清:你正在寻找一个方法'公共静态TEntityId解析(字符串值){...}'在抽象类?该方法是否是'EntityId <>'抽象的唯一原因?因为我没有真正看到它是通用*和*抽象的原因,至少在这个简化的例子中。 –