1

我试图 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 中是不可能的?是这种情况还是有解决方法?

谢谢。

4

1 回答 1

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
}

然后它应该工作。

PS 您可以在此处阅读有关泛型约束的信息。

于 2012-09-05T17:48:41.363 回答