我试图 DI 一个继承 EventArgs 的类。当我将这个类放入我拥有的一个单独的类中时,我不断收到错误消息,说“没有隐式引用转换”。
下面是 VerificationStatusEventArgs 类的声明。
namespace Jimbob.CsvImporter.DataVerification
{
public interface IVerificationStatusEventArgs
{
string SummaryMessage { get; set; }
bool CarriedOutToCompletion { get; set; }
List<String> ErrorLog { get; set; }
}
public class VerificationStatusEventArgs:EventArgs, IVerificationStatusEventArgs
{
public string SummaryMessage { get; set; }
public bool CarriedOutToCompletion { get; set; }
public List<String> ErrorLog { get; set; }
}
}
我试图将它注入到下面的类中,但不能使用接口声明事件。
public interface ICsvFileVerification
{
//Error here (1)
event EventHandler<IVerificationStatusEventArgs> VerificationCompleted;
event EventHandler UpdateProgressBar;
//..
}
public class CsvFileVerification : ICsvFileVerification
{
IUserInputEntity _entity;
IEntityVerification _entityVerification;
IVerificationStatusEventArgs _dataVerificationStatus;
public CsvFileVerification(IEntityVerification entityVerification, IVerificationStatusEventArgs dataVerificationStatus)
{
_entityVerification = entityVerification;
_dataVerificationStatus = dataVerificationStatus;
}
//Error here (2)
public event EventHandler<IVerificationStatusEventArgs> VerificationCompleted;
public event EventHandler UpdateProgressBar;
public void VerifyDataManagerAsync()
{
_dataVerificationStatus.CarriedOutToCompletion = true;
_dataVerificationStatus.ErrorLog = new List<string>();
if (!_cancelToken.IsCancellationRequested)
{
try
{
_entityVerification.VerifyUserInputManager(_entity, _dataVerificationStatus.ErrorLog);
if (_dataVerificationStatus.ErrorLog.Count > 0)
{
_dataVerificationStatus.CarriedOutToCompletion = false;
_dataVerificationStatus.SummaryMessage = "Verification of user inputs failed.";
return;
}
VerifyDataTypes();
}
catch (Exception Ex)
{
//...
}
finally
{
//Call method to raise event.
OnVerificationStatusEventArgs(this, _dataVerificationStatus);
}
}
else
{
_dataVerificationStatus.SummaryMessage = "Operation was canceled before the task could be started." + Environment.NewLine;
_dataVerificationStatus.CarriedOutToCompletion = false;
OnVerificationStatusEventArgs(this, _dataVerificationStatus);
}
}
/// <summary>
/// Raises an event on the GUI thread which should be used to notify the user that the task is
/// completed and, if relevant, the exception message that was thrown.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnVerificationStatusEventArgs(object sender, IVerificationStatusEventArgs e)
{
//Error here (3)
EventHandler<IVerificationStatusEventArgs> TempHandler = VerificationCompleted;
//Avoid possible race condition.
if (TempHandler != null)
{
TempHandler(this, e);
}
}
错误:(1)、(2)和(3):
The type 'Jimbob.CsvImporter.DataVerification.IVerificationStatusEventArgs' cannot be used as type parameter 'TEventArgs' in the generic type or method 'System.EventHandler<TEventArgs>'.
There is no implicit reference conversion from 'Jimbob.CsvImporter.DataVerification.IVerificationStatusEventArgs' to 'System.EventArgs'.
主要目的是让我可以测试 VerifyDataManagerAsync()。我想这实际上可以通过 VerificationStatusEventArgs 类的一个实例来完成。
c# 聊天中有人建议这在 .net 4.0 中是不可能的?是这种情况还是有解决方法?
谢谢。