在脚本中遇到一些奇怪的行为时,我的FileHandler类的构造函数似乎正在调用该类并运行脚本。类本身只在VS15中被引用一次,也就是由它的构造函数引用一次,main方法甚至还没有FileHandler对象,代码中的其他地方都没有提到它。
难道这段代码不应该运行吗?

编辑:我在Program.cs的开头放置了一个断点并开始单步执行,但是当我这样做的时候,我注意到public class FileHandler变成了class Program,我的构造函数被Main方法“替换”了。
这是C#设计出来的吗?

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;
}
}
}
}发布于 2016-07-31 21:52:31
我知道是怎么回事了。由于某种原因,我在系统的Documents文件夹中备份了一个旧版本的TradingDaysFileChecker.cs。在该版本中,所有文件处理逻辑都在Program.cs中。我重构并将文件处理提取到新类中。
由于某些原因,当我运行脚本时,它仍然在使用旧的副本,即使它不在解决方案文件夹中。这就是为什么类名发生变化的原因,它跳转到另一个文件的Program.cs和Main方法中,从我的Documents文件夹中拉出它。这是怎么发生的,我不知道。
我从我的Documents文件夹中删除了该文件,现在它运行正常。
https://stackoverflow.com/questions/38684352
复制相似问题