有什麼辦法可以將的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 <>'抽象的唯一原因?因爲我沒有真正看到它是通用*和*抽象的原因,至少在這個簡化的例子中。 –