Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 强制转换事件的发送方以在触发事件的字典中查找值_C#_Dictionary_.net 4.5 - Fatal编程技术网

C# 强制转换事件的发送方以在触发事件的字典中查找值

C# 强制转换事件的发送方以在触发事件的字典中查找值,c#,dictionary,.net-4.5,C#,Dictionary,.net 4.5,在我的类中,我有一个私有C#字典,其中键是一个字符串,值是一个具有PropertyChanged事件的对象。定义如下: private readonly Dictionary<string, IAlsVariable> m_operatorAudibleSevServerVariables = new Dictionary<string, IAlsVariable>(); 如您所见,在将每个变量(值)的实例添加到字典之前,我还订阅了每个变量的PropertyChange

在我的类中,我有一个私有C#字典,其中键是一个字符串,值是一个具有
PropertyChanged
事件的对象。定义如下:

private readonly Dictionary<string, IAlsVariable> m_operatorAudibleSevServerVariables = new Dictionary<string, IAlsVariable>();
如您所见,在将每个变量(值)的实例添加到字典之前,我还订阅了每个变量的
PropertyChanged
事件

现在,只要变量的一个实例触发
PropertyChanged
事件,我就会执行(一)个事件处理程序。问题是我不知道字典中的哪些变量触发了
PropertyChanged
事件

我有一个名为
UpdateAlarmSound
的方法,每当此事件触发时,我都需要调用该方法。我需要传递给这个方法的第一个参数是字典中相关变量的键

我的第一个猜测是强制转换变量,然后使用该值搜索键。但我不知道这样做是否可靠。下面的代码就是我所拥有的。这似乎是有效的,但我有一些怀疑,也许有另一个更好的方法。也许我的方法可行,但错了。也许我应该使用
.Equals
而不是
=

private void OpcVariable_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    var opcVariable = sender as IAlsVariable;
    var key = m_operatorAudibleSevServerVariables.FirstOrDefault(x => x.Value == opcVariable).Key;
    UpdateAlarmSound(key, Convert.ToInt32(opcVariableVtqValue));            
}

事实上,它确实起到了作用。一个改进是将
属性更改的事件信息包装在一个新的自定义
事件和一个新的
EventArgs
中,并具有该事件的
名称和
opcVariable
实例部分。这样可以避免任何可能导致运行时错误的强制转换

下面是
EventArgs
的一个示例:

public class YourEventArgs : EventArgs
{
    public string Name {get;set;}

    public int OpcVariableVtqValue  {get;set;}

    // If you still need it.
    public string PropertyName {get;set;}
}
以下是在IAlsVariable中对所做的更改:

public interface IAlsVariable
{
    event EventHandler<YourEventArgs> YourEvent;
}

当您的主类被释放以避免内存泄漏时,不要忘记取消订阅活动。

我当然想改进这一点,但我不是基于事件编程的专家。新的定制活动是什么意思?如何包装PropertyChanged事件?@Ray我已更新答案,以包含如何引发事件的示例。你必须弄清楚什么是OpcVariableVtqValue,因为我不确定它是从哪里来的。希望这个答案能给你足够的信息:)
public interface IAlsVariable
{
    event EventHandler<YourEventArgs> YourEvent;
}
public class SomeAlsVariableImpl : IAlsVariable
{
    public string Name {get;}

    public event EventHandler<YourEventArgs> YourEvent;

    //  Call this method any time you need to raise the event.
    private void RaiseYourEvent(int opcVariableVtqValue)
    {
        YourEvent?.Invoke(this, new YourEventArgs 
        {
             Name = Name,
             OpcVariableVtqValue = opcVariableVtqValue,
        }
    }
}  
        foreach (var clusterManager in clusterManagers)
        {
            var clusterName = clusterManager.Name;
            string opcDataSourceServer = "server/" + clusterName + ":[2:http://www.alstom.com/Transport/Iconis/S2K/Data]<Organizes>2:S2KServer<Organizes>2:S2KTerritoryMngt<Organizes>2:TASSObject<HasComponent>2:OperatorAudibleSev";
            var opcVariable = m_alsApplication.Database.GetVariable(opcDataSourceServer, null);
            opcVariable.YourEvent += OpcVariable_YourEvent;
            m_operatorAudibleSevServerVariables.Add(clusterManager.Name, opcVariable);                
        }
private void OpcVariable_YourEvent(object sender, YourEventArgs e)
{
    UpdateAlarmSound(e.Name, e.OpcVariableVtqValue));            
}