C# 在使用基本构造函数时避免重复
C# avoid repetition when working with base constructors
有两个class,NodeBase和继承自抽象class NodeBase的ContentSectionNode,我想知道是否有任何方法可以避免在ContentSectionNode中重复一段代码构造函数,同时也委托给基础 class 构造函数。
抽象 NodeBase class 构造函数如下所示:
protected NodeBase(string tagType, string content)
: this()
{
TagType = tagType;
Content = content;
}
protected NodeBase(Guid? parentId, int? internalParentId, string tagType, string content)
: this(tagType, content)
{
ParentId = parentId;
InternalParentId = internalParentId;
}
ContentSectionNode class 构造函数如下所示:
public ContentSectionNode(Guid createdBy)
: this()
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
public ContentSectionNode(Guid createdBy, string tagType, string content)
:base(tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
public ContentSectionNode(Guid createdBy, Guid? parentId, int? internalParentId, string tagType, string content)
: base(parentId, internalParentId, tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
我想知道是否有任何方法可以避免重复
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
阻止 ContentSectionNode 的所有 ctors class。
请注意,_createdBy、_createdAt 和 UpdatedBy、UpdatedAt fields/props 只能从 ContentSectionNode class 访问并且只能在此处设置。
该项目使用的是 C# 5.0,因此没有自动 属性 初始化程序。
谢谢!
像这样?
public ContentSectionNode(Guid createdBy)
: this(createdBy,null,null, null, null)
{
}
public ContentSectionNode(Guid createdBy, string tagType, string content)
: this(createdBy, null, null tagType, contect)
{
}
public ContentSectionNode(Guid createdBy, Guid? parentId, int? internalParentId, string tagType, string content)
: base(parentId, internalParentId, tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
有两个class,NodeBase和继承自抽象class NodeBase的ContentSectionNode,我想知道是否有任何方法可以避免在ContentSectionNode中重复一段代码构造函数,同时也委托给基础 class 构造函数。
抽象 NodeBase class 构造函数如下所示:
protected NodeBase(string tagType, string content)
: this()
{
TagType = tagType;
Content = content;
}
protected NodeBase(Guid? parentId, int? internalParentId, string tagType, string content)
: this(tagType, content)
{
ParentId = parentId;
InternalParentId = internalParentId;
}
ContentSectionNode class 构造函数如下所示:
public ContentSectionNode(Guid createdBy)
: this()
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
public ContentSectionNode(Guid createdBy, string tagType, string content)
:base(tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
public ContentSectionNode(Guid createdBy, Guid? parentId, int? internalParentId, string tagType, string content)
: base(parentId, internalParentId, tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
我想知道是否有任何方法可以避免重复
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
阻止 ContentSectionNode 的所有 ctors class。 请注意,_createdBy、_createdAt 和 UpdatedBy、UpdatedAt fields/props 只能从 ContentSectionNode class 访问并且只能在此处设置。
该项目使用的是 C# 5.0,因此没有自动 属性 初始化程序。 谢谢!
像这样?
public ContentSectionNode(Guid createdBy)
: this(createdBy,null,null, null, null)
{
}
public ContentSectionNode(Guid createdBy, string tagType, string content)
: this(createdBy, null, null tagType, contect)
{
}
public ContentSectionNode(Guid createdBy, Guid? parentId, int? internalParentId, string tagType, string content)
: base(parentId, internalParentId, tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}