using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace SimpleMock.Core.Events
{
public class EventRaiserHelper<T>
{
public MemberInfo GetEvent(IEventRaisingAgent eventRaisingAgent, object proxy, Action<T> eventToRaise)
{
PredictiveAnalyzer<T> predictiveAnalyzer = new PredictiveAnalyzer<T>(proxy, eventToRaise, AnalyzerType.Event);
predictiveAnalyzer.Analyze();
return predictiveAnalyzer.Invocation;
}
public void RaiseEvent(IEventRaisingAgent eventRaisingAgent, bool isCustomEvent, object proxy, object[] args, MemberInfo member)
{
List<object> delegateArgs = new List<object>();
if (!isCustomEvent)
{
delegateArgs.Add(proxy);
}
delegateArgs.AddRange(args);
if (eventRaisingAgent.AllEventsForProxy.ContainsKey(member.Name))
{
foreach (Delegate handler in eventRaisingAgent.AllEventsForProxy[member.Name].InvocationList)
{
handler.Method.Invoke(handler.Target, delegateArgs.ToArray());
}
}
}
public void GetAndRaiseEvent(IEventRaisingAgent eventRaisingAgent, object proxy, Action<T> eventToRaise, object[] args)
{
List<object> delegateArgs = new List<object>() { proxy };
delegateArgs.AddRange(args);
PredictiveAnalyzer<T> predictiveAnalyzer = new PredictiveAnalyzer<T>(proxy, eventToRaise, AnalyzerType.Event);
predictiveAnalyzer.Analyze();
MemberInfo member = predictiveAnalyzer.Invocation;
foreach (Delegate handler in eventRaisingAgent.AllEventsForProxy[member.Name].InvocationList)
{
handler.Method.Invoke(handler.Target, delegateArgs.ToArray());
}
}
public void GetAndRaiseEventCustomArgs(IEventRaisingAgent eventRaisingAgent, object proxy, Action<T> eventToRaise, object[] args)
{
PredictiveAnalyzer<T> predictiveAnalyzer = new PredictiveAnalyzer<T>(proxy, eventToRaise, AnalyzerType.Event);
predictiveAnalyzer.Analyze();
MemberInfo member = predictiveAnalyzer.Invocation;
foreach (Delegate handler in eventRaisingAgent.AllEventsForProxy[member.Name].InvocationList)
{
handler.Method.Invoke(handler.Target, args);
}
}
}
}