2016-07-31 4 views
0

スクリプトで奇妙な動作が発生した場合、FileHandlerクラスのコンストラクタがクラスを呼び出してスクリプトを実行しているようです。 クラス自体はVS15で一度しか参照されておらず、そのコンストラクタによって参照されていますが、メインメソッドにはFileHandlerのオブジェクトもありません。コードのどこにも記述されていません。C#初期化および実行中のスクリプトのクラスのコンストラクタ

このコードは実行しないでください。

enter image description here

編集:私はProgram.csの先頭にブレークポイントを配置し、通じステッピング始めたが、私はこれをしたとき、私はpublic class FileHandlerclass Programになり、私のコンストラクタがMain方法によって「置き換え」されていることに気づきました。

これはC#が設計したものですか?

enter image description here

Programs.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using QuantConnect; 
using QuantConnect.Securities; 
using QuantConnect.Securities.Forex; 

namespace TradingDaysFileChecker 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var securityType = SecurityType.Forex; 
      var ticker = TickType.Trade; 
      var marketHoursDatabase = MarketHoursDatabase.FromDataFolder(); 
      var market = Market.FXCM; 
      var symbol = Symbol.Create(ticker.ToString(), securityType, market); 
      var marketHoursDbEntry = marketHoursDatabase.GetEntry(symbol.ID.Market, symbol.Value, symbol.ID.SecurityType); 
      var exchange = new ForexExchange(marketHoursDbEntry.ExchangeHours); 


      Console.ReadLine(); 
     } 


    } 
} 

FileHandler.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using QuantConnect.Securities.Forex; 

namespace TradingDaysFileChecker 
{ 
    public class FileHandler 
    { 

     private readonly StreamWriter _writeToFile; 
     private readonly List<Tuple<string, string>> _missingDays; 
     private readonly string _dataFilePath; 
     private readonly DateTime _startDate; 
     private readonly DateTime _endDate; 
     private readonly ForexExchange _exchange; 
     private readonly IEnumerable<DateTime> _validTradingDays; 
     private readonly string[] _forexSecuritiesFolders; 

     public FileHandler(ForexExchange exchange) 
     { 
      _startDate = new DateTime(2007, 04, 01); 
      _endDate = new DateTime(2016, 07, 25); 
      _exchange = exchange; 

      _writeToFile = new StreamWriter(@"C:\Users\RichardsPC\Documents"); 
      _dataFilePath = @"C:\Users\RichardsPC\Desktop\export\exporter\forex\fxcm\minute\"; 
      _forexSecuritiesFolders = Directory.GetDirectories(_dataFilePath); 

      _missingDays = new List<Tuple<string, string>>(); 
      _validTradingDays = IterateOverDateRange(_exchange, _startDate, _endDate); 
     } 

     public void CheckForMissingFiles() 
     { 
      foreach (var validDay in _validTradingDays) 
      { 
       foreach (var forexSecurity in _forexSecuritiesFolders) 
       { 
        var fxPair = new DirectoryInfo(forexSecurity).Name; 
        var formattedDate = FormatDate(validDay); 
        var path = SetPath(_dataFilePath, fxPair, formattedDate); 

        if (!File.Exists(path)) 
        { 
         _missingDays.Add(Tuple.Create(fxPair, formattedDate)); 
        } 
       } 
      } 

      Results(); 
     } 

     public void Results() 
     { 
      if (_missingDays.Count > 0) 
      { 
       foreach (var missingDay in _missingDays.OrderBy(md => md.Item1)) 
       { 
        var formattedTupleOutput = missingDay.ToString().TrimStart('(').TrimEnd(')'); 
        Console.WriteLine(formattedTupleOutput); 
        WriteResultsToFile(formattedTupleOutput); 
       } 
      } 

      else 
      { 
       var noFilesMissing = "No results missing"; 
       Console.WriteLine(noFilesMissing); 
       WriteResultsToFile(noFilesMissing); 
      } 

      Console.WriteLine("Records missing: " + _missingDays.Count); 
     } 

     public void WriteResultsToFile(string result) 
     { 
      _writeToFile.WriteLine(result); 
     } 

     public static string FormattedFileName(string tradingDay) 
     { 
      return tradingDay + "_quote.zip"; 
     } 

     public string FormatDate(DateTime validDay) 
     { 
      return validDay.ToString("yyyyMMdd"); 
     } 

     public static string SetPath(string dataFilePath, string fxPair, string formattedDate) 
     { 
      return dataFilePath + fxPair + @"\" + FormattedFileName(formattedDate); 
     } 

     public IEnumerable<DateTime> IterateOverDateRange(ForexExchange exchange, DateTime start, DateTime end) 
     { 
      for (var day = start.Date; day.Date <= end.Date; day = day.AddDays(1)) 
       if (exchange.IsOpenDuringBar(day.Date, day.Date.AddDays(1), false)) 
       { 
        yield return day; 
       } 
     } 
    } 
} 
+0

あなたはそれが実行されていると思いますか? "参照"はコンストラクタだけです(そしてそれは正常です)。 – pinkfloydx33

+0

コードにブレークポイントを置き、メニュー:デバッグ:Windows:コールスタックを使用してコードを呼び出すメソッドを検索します。 – jdweng

+0

また、クラスは 'IDisposable'を実装する必要があります。これは' StreamWriter'/'FileStream'を保持し、そのStreamWriterで' Dispose'を呼び出す必要があります。 – pinkfloydx33

答えて

0

私は何が起こっているかを考え出しました。 何らかの理由でバックアップした私のシステムのDocumentsフォルダに古いバージョンのTradingDaysFileChecker.csがありました。 そのバージョンでは、すべてのファイル処理ロジックはProgram.csの内部にありました。 新しいクラスのファイル処理をリファクタリングして抽出しました。

私はスクリプトを実行していたときに何らかの理由で、ソリューションフォルダにないにもかかわらず、その古いコピーを使用していました。 そのため、クラス名の変更が起こったようですが、他のファイルのProgram.csMainメソッドにジャンプしていました。これをDocumentsフォルダから取得しています。 どうしたのですか、わかりません。

私はDocumentsフォルダからファイルを削除しましたが、正しく動作しています。

関連する問題