例子:
public class EmailBusinessLogic
{
#region Fields and Constructors
SmtpClient smtp;
Parameter prm;
public EmailBusinessLogic()
{
prm = CostHelper.GetParameter();
smtp = new SmtpClient(prm.EmailHost, prm.EmailPort);
smtp.UseDefaultCredentials = prm.EmailUseDefaultCredentials;
smtp.DeliveryMethod = GetDeliveryMethod(prm.EmailDeliveryMethod); //CALL TO METHOD DOWN BELOW, IS THIS A GOOD PRACTICE?
smtp.EnableSsl = prm.EmailEnableSSL;
smtp.Credentials = new NetworkCredential(prm.AppUserName, prm.AppPass, prm.AppNetworkDomain);
}
#endregion
#region Instance Methods
public SmtpDeliveryMethod GetDeliveryMethod(string name)
{
switch (name)
{
case "Network": return SmtpDeliveryMethod.Network;
case "IISDirectory": return SmtpDeliveryMethod.PickupDirectoryFromIis;
case "OtherDirectory": return SmtpDeliveryMethod.SpecifiedPickupDirectory;
default: throw new NonExistentObjectException();
}
}
我问这个是因为它是一个小悖论,我知道在将此类实例化为新对象时总是首先调用构造函数。而且我不想让它成为一个静态方法,因为这将创建一个不会被垃圾收集的静态实例。
如果我错了,请纠正我,非常欢迎有经验的程序员同行给出明确的答案。谢谢你。