我在使用关联类型作为协议时遇到问题:
private static TelemetryClient telemetryClient = new TelemetryClient();
private static System.Timers.Timer aTimer;
static void Main(string[] args)
{
// Register Unhandled Exception Handler
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// Set the Instrumentation Key to track the WebJob Exception
TelemetryConfiguration.Active.InstrumentationKey = SecureConfigReader.GetSetting("ApplicationInsightsKey");
Configure();
if (args.Length > 0)
{
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
double Scheduled_Interval = Convert.ToDouble(args[0]);
// Set the Interval to 300 seconds.
aTimer.Interval = TimeSpan.FromSeconds(Scheduled_Interval).TotalMilliseconds;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
RunTests();
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
ExceptionTelemetry excTelemetry = new ExceptionTelemetry((Exception)e.ExceptionObject);
excTelemetry.SeverityLevel = SeverityLevel.Critical;
excTelemetry.HandledAt = ExceptionHandledAt.Unhandled;
telemetryClient.InstrumentationKey = SecureConfigReader.GetSetting("ApplicationInsightsKey");
telemetryClient.TrackException(excTelemetry);
//make sure all telemetry is sent before closing the app
telemetryClient.Flush();
}
/*
* 1. Find all tests
* 2. Run all tests, collecting all results
* 3. Process alerts if any
* 4. Save results
*/
private static void RunTests()
{
List<SyntheticTransactionTableEntity> results = new List<SyntheticTransactionTableEntity>();
List<ISyntheticTransaction> tests = new List<ISyntheticTransaction>();
}
当我尝试实现搜索结果协议时,我遇到编译问题:
“类型SearchArticles不符合协议SearchResultsProtocol”
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.fetchedUser), name: notificationName, object: nil)
据我了解,这是因为SearchArticles类中的T不是来自具体类型(该示例中的struct),而是来自协议类型。
有没有办法解决这个问题?
提前致谢!
答案 0 :(得分:2)
associatedtype
并不是一个占位符(协议),它应该是一个具体的类型。通过在类声明中添加泛型,您可以获得与此相同的结果....
class SearchArticles<V: ArticleProtocol>: SearchResultsProtocol {
typealias T = V
}
然后,当您在应用周围使用SearchArticles
时,您可以声明let foo = SearchArticles<FirstArticle>
或let foo = SearchArticles<SecondArticle>