Reputation: 7591
I have one project, and in that project I have loaded a dll dynamically. It looks like:
AssemblyPart assemblyPart = new AssemblyPart();
WebClient downloader = new WebClient();
string path = string.Format("../ClientBin/{0}.xap", "AgileTax");
downloader.OpenReadCompleted += (e, sa) =>
{
getdllinStream = GetStream(sa.Result, _CurReturnType.PackageName + "ERC", true);
_formsAssembly = assemblyPart.Load(getdllinStream);
foreach (var item in _formsAssembly.GetTypes())
{
if (item.FullName == _CurReturnType.PackageName + "ERC.State.StateMain")
{
ATSpgm = item;
}
}
var class_instance = _formsAssembly1.CreateInstance(PackageName + "ERC.State.StateMain");
if (class_instance != null)
{
MethodInfo[] infomem = ATSpgm.GetMethods();
MethodInfo SetVarDllNG1 = ATSpgm.GetMethod("ProcessERC");
SetVarDllNG1.Invoke(class_instance, parametersArray);
}
}
downloader.OpenReadAsync(new Uri(path, UriKind.Relative));
Now my problem is that in the .dll I have code like:
public event ERCErrorHandling OnERCErrorHandler;
public delegate string ERCErrorHandling(Exception ex);
Now my question is how to call this ERCErrorHandling event same above i have called method like ProcessERC.
Upvotes: 0
Views: 350
Reputation: 7468
An event is just a Field of type MulticastDelegate. This means that you can get it with these instructions:
FieldInfo anEvn = item.GetType().GetField("OnERCErrorHandler",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic) as FieldInfo;
if (anEvn != null)
MulticastDelegate mDel = anEvn.GetValue(item) as MulticastDelegate;
Then you can get the single delegates attached to the event using GetInvokationList(). Each delegate has a Target (the object that will execute the method) and the Method. You can loop through them executing them all. Of course you have to know the expected parameters in order to pass them to Invoke:
Delegate[] dels = mDel.GetInvocationList();
object[] parms = new object[1];
parms[0] = null; // you must create the Exception you want to pass as argument here
foreach (Delegate aDel in dels)
aDel.Method.Invoke(aDel.Target, parms);
Upvotes: 1