我试图去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)&amp; (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#chat中有人建议在.net 4.0中这不可行吗?是这种情况还是有解决方法?
感谢。
答案 0 :(得分:2)
定义:
delegate System.EventArgs<TEventArgs> where TEventArgs : EventArgs
表示括号内的任何内容都必须从EventArgs
继承。
IVerificationStatusEventArgs
不会继承EventArgs
。您可以通过创建继承自EventArgs
的抽象类来继承您的继承链。然后继承它。
public abstract class VerificationStatusEventArgsBase : EventArgs
{
//some abstract methods here
}
public class VerificationStatusEventArgs : VerificationStatusEventArgsBase
{
//implement your abstract methods
}
然后它应该工作。
P.S。您可以阅读有关泛型here的约束。