Feel free to question my sanity.
I need to determine if an Action<T> vs Action<T> is the original instance. What I have is a class with a class variable protected Action<T> MessageCallback = null; when my abstract class Message<T> is created via an abstract method I force "them" to initialize the MessageCallBack. This MessageCallback gets added to a IList<Action<object>>. Each action defined in this list can be different. Now, what I want to do is remove a specific action from the list but am failing in my attempts to compare it.
Below is a sample of the last setup I have attempted:
public void Unsubscribe<TMessage>(Action<TMessage> messageCallback)
{
var messageType = typeof(TMessage);
var callbackTypes = messageReceivedCallbacks
.Keys
.Where(k => k.IsAssignableFrom(messageType));
lock (messageReceivedCallbacks)
{
foreach (var callbackType in callbackTypes)
{
messageReceivedCallbacks[callbackType].Remove(new Action<object>(m =>
messageCallback((TMessage)m)));
}
}
}
I understand what I want to do may not be possible but generally I'm just doing something an improper way or lack proper knowledge to do it like I am suppose to. Thanks in advance for any help you provide.
- UPDATE after trying some of the methods below:
Comparing them keeps failing. None of the currently 3 suggestions below work. I do believe I can change how I am handling this and make it work how I need it to by passing in a key with the action that then points to separate list of <key, indexOfAction> then removing that by index. However, I feel like I still need to give this good