C#提供正确的用法和调用

本文关键字:用法 调用 | 更新日期: 2023-09-27 18:30:06

这可能是一个新手问题,所以我会直接说出来。这是我第一次创建IDisposable类,我想确保我正确地创建了我的类,正确地调用了它,并正确地处理了它。谢谢

使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Text;命名空间爬网程序{class LoggingClass{公共类LoggingDisposable:IDisposable{public void GenericLogging(字符串systemMsg,字符串SystemClass,字符串SystemSection,字符串ID,字符串FixID,字符串baseURL,字符串mysqlQueryName,字符串mysqlQuery){string Loggingall="插入tblLogs"+"set SystemMsg='"+SystemMsg.Replace("","")+"'"+",SystemClass='"+SystemClass.Replace("","")+"'"+",SystemSection="+SystemSection.Replace("","")+",ID='"+CarID.Replace("","")+"'"+",FixID='"+FixID.Replace("","")+"'"+",baseurl='"+baseurl.Replace("","")+"'"+",mysqlqueryName='"+mysqlQuery.Replace("","")+",mysqlquery='"+mysqlquery.Replace("","")+"'"+",Site='AutoTrader'"+",TimeStamp=Now()";使用(var MYSQLP=new MySQLProcessing.MySQLProcessor()){MYSQLP.MySQLInsertUpdate(Loggingall,"Loggingall");}}public void Dispose(){这处置(true);GC.SuppressFinalize(this);}受保护的虚拟void Dispose(bool Dispose){if(!this.Disposed){如果(处置){//在此处执行托管清理。}//在此处执行非托管清理。这Disposed=true;}}protected bool已处置{get;private set;}}}}

我就是这么称呼它的。

var options=new ParallelOptions();选项。最大平行度=5;Parallel.ForEach(urlTable.AsEnumerable(),选项,drow=>{
 using (var logClass = new LoggingClass.LoggingDisposeable())
                                {
                                    logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode",rDealerLink, "", "");
}
}</pre>

C#提供正确的用法和调用

我想你打算做的是这样的事情:

public class LoggingDisposeable : IDisposable
{
    MySQLProcessing MySQLP;
    public LoggingDisposeable()
    {
        MYSQLP = new MySQLProcessing.MySQLProcessor();
    }
    public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery)
    {
        string Loggingall = " insert into tblLogs " +
                        "set SystemMsg='" + systemMsg.Replace("'", "''") + "'" +
                        ",SystemClass = '" + SystemClass.Replace("'", "''") + "'" +
                        ",SystemSection = '" + SystemSection.Replace("'", "''") +
                        ",ID = '" + CarID.Replace("'", "''") + "'" +
                        ",FixID = '" + FixID.Replace("'", "''") + "'" +
                        ",baseurl = '" + baseURL.Replace("'", "''") + "'" +
                        ",mysqlqueryName = '" + mysqlQuery.Replace("'", "''") +
                        ",mysqlquery = '" + mysqlQuery.Replace("'", "''") + "'" +
                        ",Site = 'AutoTrader'" +
                        ",TimeStamp = Now()";
            MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");                
    }
    public void Dispose()
    {
        MYSQLP.Dispose();
    }
}

然后这样使用:

using (var logClass = new LoggingDisposeable())
{
    var options = new ParallelOptions();
    options.MaxDegreeOfParallelism = 5;
    Parallel.ForEach(urlTable.AsEnumerable(), options, drow =>
        {
            logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode", rDealerLink, "", "");
        });
}