我试图弄清楚在调用派生类构造函数时如何调用基类构造函数。
我有一个名为“AdditionalAttachment”的类,该类继承自System.Net.Mail.Attachment。我在新类中添加了另外2个属性,以便可以将现有Attachment类的所有属性与新属性一起使用
public class AdditionalAttachment: Attachment
{
[DataMember]
public string AttachmentURL
{
set;
get;
}
[DataMember]
public string DisplayName
{
set;
get;
}
}
之前我曾经创建过类似的构造函数
//objMs是一个MemoryStream对象
Attachment objAttachment = new Attachment(objMs, "somename.pdf")
我想知道如何为我的类创建相同类型的构造函数,该类构造函数的作用与上述基类的构造函数相同
最佳答案
这会将您的参数传递给基类的构造函数:
public AdditionalAttachment(MemoryStream objMs, string displayName) : base(objMs, displayName)
{
// and you can do anything you want additionally
// here (the base class's constructor will have
// already done its work by the time you get here)
}