c# - How to Implement Dispose In Inheriting Class -
i using smtpclient in .net-3.5 framework (meaning not implement idisposable), put in using statement so:
using (var client = new disposablesmtpclient("mail.domain.com", 25) { credentials = new networkcredential(), enablessl = false }) { client.send(emailaddress, emailaddress, subject, body); } so created following:
class disposablesmtpclient : smtpclient, idisposable { bool disposed; public disposablesmtpclient(string mailserver, int port) : base(mailserver, port) { // var client = new smtpclient(mailserver, port); } public void dispose() { this.dispose(); gc.suppressfinalize(this); } } which works fine sending message, when debugging, throws stackoverflow exception (as this.dispsoe(); call forever).
i tried calling this.dispose(true) per many other questions, complains no overload method 'dispose' takes 1 arguments.
base.dispoe() work, because of course 'smtpclient' not conatin definition 'dispose'
finally, tried signature protected override void dispose(bool disposing), dispose(): no suitable method found override
is able point me in right direction this?
1 - don't need dispose anything, don't need dispose.
just go new:
var client = new smtpclient("mail.domain.com", 25) { credentials = new networkcredential(), enablessl = false }; client.send(emailaddress, emailaddress, subject, body); 2 - if really want use using (and don't know why should), have implement empty dispose (because don't have dispose begin with):
class disposablesmtpclient : smtpclient, idisposable { public disposablesmtpclient(string mailserver, int port) : base(mailserver, port) { } public void dispose() { // do, don't anything. } } imo, kiss principle should consider.
Comments
Post a Comment