无法访问要在HandleRequest中使用的EventArgs e值

Ton*_*ick 1 c# events eventargs

我只是在了解活动,代表和订阅者.我花了最近两天的时间来研究和包裹我的大脑.我无法访问EventArgs e值中传递的信息.我有一个想要打开的已保存项目.必要形式的状态被反序列化为字典.触发一个循环,引发UnpackRequest传递键/值.

ProjectManager.cs文件:

public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs;
public event EventHandler<UnpackEventArgs> UnpackRequest;
Run Code Online (Sandbox Code Playgroud)

然后再往下走:

ProjectManager.cs文件:

//Raise a UnpackEvent //took out virtual
protected  void RaiseUnpackRequest(string key, object value)
{
    if (UnpackRequest != null) //something has been added to the list?
    {
        UnpackEventArgs e = new UnpackEventArgs(key, value);
        UnpackRequest(this, e);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在open方法中,在使用每个表单的状态填充字典之后:

ProjectManager.cs文件:

foreach (var pair in dictPackState) {
    string _key = pair.Key;
    dictUnpackedState[_key] = dictPackState[_key];
    RaiseUnpackRequest(pair.Key, pair.Value); //raises the event.
    }
Run Code Online (Sandbox Code Playgroud)

我有一个活动课:

public class UnpackEventArgs : EventArgs
{
    private string strKey;
    private object objValue;

    public UnpackEventArgs(string key, object value)
    {
        this.strKey = key;
        this.objValue = value;
    }
    //Public property to read the key/value ..and get them out
    public string Key
    {
        get { return strKey; }  
    }
    public object Value
    { 
        get { return objValue; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我仍在研究添加订阅者的代码以及如何在各个表单中重新构建项目组件.但是我现在正在尝试处理的部分是在MainForm.cs中,我使用争论传递来处理解压缩请求.我的e包含键值,键表示发送值的位置(表单对象).

private void HandleUnpackRequest(object sender, EventArgs e)
{
    //reflection happens here. turn key into object
    //why can't i get e.key ??? 
    object holder = e; //holder = VBTools.UnpackEventArgs key... value...all info

    //object goToUnpack = (e.key).GetType();
    //goToUnpack.unpackState(e.value);
}
Run Code Online (Sandbox Code Playgroud)

我想我提供了所有必要的部分以获得任何帮助?!谢谢!

Alf*_*dBr 6

改变这个:

private void HandleUnpackRequest(object sender, EventArgs e) 
Run Code Online (Sandbox Code Playgroud)

对此:

private void HandleUnpackRequest(object sender, UnpackEventArgs e) 
Run Code Online (Sandbox Code Playgroud)

记住您的事件处理程序声明

public event EventHandler<UnpackEventArgs> UnpackRequest;
Run Code Online (Sandbox Code Playgroud)