英文:
How can I add an existing delegate to an event?
问题
我要翻译的内容如下:
"I have an object that has events. I want to reset that object to default but keep the existing event subscriptions. My aim is to have a bunch of subscriptions to events on this object (_myRepo), then at some point I want to set _myRepo = null, create a new copy of the object with its default state and then add back all the previously saved subscribers. I use a generic approach instead of strong typing due to there being many repos in this class.
The issue I'm having is trying to add the subscription back to the event once it's has been recreated. I'm trying to do this in the ApplyCachedEventHandlers() function. How do I add this delegate back to the new object's event?
This is what I'm doing so far...
private IMyRepo _myRepo;
private Dictionary<Type, Delegate> _subscribers = new Dictionary<Type, Delegate>();
public IMyRepo MyRepo
{
get
{
if (_myRepo == null)
_myRepo = new MyRepo(_transaction);
ApplyCachedEventHandlers(_myRepo);
return _myRepo;
}
}
private void ResetRepo()
{
CacheEventHandlers(_myRepo);
_myRepo = null;
}
private void CacheEventHandlers
{
if (repo == null)
return;
Type t = repo.GetType();
FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
foreach (FieldInfo fi in fia)
{
Delegate d = fi.GetValue(repo) as Delegate;
if (d != null)
{
foreach (Delegate subscriber in d.GetInvocationList())
_subscribers.Add(t, subscriber);
}
}
}
private void ApplyCachedEventHandlers
{
if (repo == null || _subscribers == null || _subscribers.Count < 1)
return;
Type t = repo.GetType();
FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
foreach (KeyValuePair<Type, Delegate> subscriber in _subscribers)
{
if (t == subscriber.Key)
{
foreach (FieldInfo fi in fia)
{
// This is where I get hung up
Debug.WriteLine("Add the saved delegate to the subscriptions of the event (fi).");
}
}
}
}"
希望这对你有所帮助。
英文:
I have an object that has events. I want to reset that object to default but keep the existing event subscriptions. My aim is to have a bunch of subscriptions to events on this object (_myRepo), then at some point I want to set _myRepo = null, create a new copy of the object with it's default state and then add back all the previously save subscribers. I use a generic approach instead of strong typing due to their being many repos in this class.
The issue I'm having is trying to add the subscription back to the event once it's has been recreated. I'm trying to do this in the ApplyCachedEventHandlers() function. How do I add this delegate back to the new object's event?
This is what I'm doing so far...
private IMyRepo _myRepo;
private Dictionary<Type, Delegate> _subscribers = new Dictionary<Type, Delegate>();
public IMyRepo MyRepo
{
get
{
if (_myRepo == null)
_myRepo = new MyRepo(_transaction);
ApplyCachedEventHandlers(_myRepo);
return _myRepo;
}
}
private void ResetRepo()
{
CacheEventHandlers(_myRepo);
_myRepo = null;
}
private void CacheEventHandlers<T>(T repo)
{
if (repo == null)
return;
Type t = repo.GetType();
FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
foreach (FieldInfo fi in fia)
{
Delegate d = fi.GetValue(repo) as Delegate;
if (d != null)
{
foreach (Delegate subscriber in d.GetInvocationList())
_subscribers.Add(t, subscriber);
}
}
}
private void ApplyCachedEventHandlers<T>(T repo)
{
if (repo == null || _subscribers == null || _subscribers.Count < 1)
return;
Type t = repo.GetType();
FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
foreach (KeyValuePair<Type, Delegate> subscriber in _subscribers)
{
if (t == subscriber.Key)
{
foreach (FieldInfo fi in fia)
{
// This is where I get hung up
Debug.WriteLine("Add the saved delegate to the subscriptions of the event (fi).");
}
}
}
}
答案1
得分: 0
如果您想通过反射订阅事件,您不应该使用字段,而应该使用事件:
var eventInfos = type.GetEvents(); // 根据需要传递标志
foreach (KeyValuePair<Type, Delegate> subscriber in _subscribers)
{
if (t == subscriber.Key)
{
foreach (EventInfo ei in eventInfos)
{
ei.AddEventHandler(repo, subscriber.Value);
}
}
}
英文:
If you want to subscribe to event via reflection you should not work with fields, but with events:
var eventInfos = type.GetEvents(); // pass the flags if needed
foreach (KeyValuePair<Type, Delegate> subscriber in _subscribers)
{
if (t == subscriber.Key)
{
foreach (EventInfo ei in eventInfos)
{
ei.AddEventHandler(repo, subscriber.Value);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论