Event/Delegate Emit and Reflection
-
I'm working on the basis for a CodeProject article, but stuck on a couple of issues. The current problem is as follows: I have to dynamically create a method that can be used as an event handler for a particular event, given only the
EventInfo
object that represents the event. This includes a reference to the type (subclassed fromDelegate
and accessed through theEventInfo.EventHandlerType
property) that is created to represent the method in delegate form. However, there doesn't seem to be an obvious way to read out the desired method form (parameters & parameters' types, return type, etc.). Does anyone know how to get this method semi-signature out of the EventInfo, the event handlerType
, or anything else that's dynamically accessible through the framework (almost certainly through Reflection)? Thanks! Eric Astor -
I'm working on the basis for a CodeProject article, but stuck on a couple of issues. The current problem is as follows: I have to dynamically create a method that can be used as an event handler for a particular event, given only the
EventInfo
object that represents the event. This includes a reference to the type (subclassed fromDelegate
and accessed through theEventInfo.EventHandlerType
property) that is created to represent the method in delegate form. However, there doesn't seem to be an obvious way to read out the desired method form (parameters & parameters' types, return type, etc.). Does anyone know how to get this method semi-signature out of the EventInfo, the event handlerType
, or anything else that's dynamically accessible through the framework (almost certainly through Reflection)? Thanks! Eric AstorThis might get you on the right track:
Assembly assembly.LoadFrom("my_assembly.dll");
Type type=assembly.GetType("my_class");
MethodInfo mi=type.GetMethod("my_method");Using MethodInfo, you can inspect the security, return type, parameters, etc. Lots of fun. [edit]Oops--LoadFrom, not GetType[/edit] Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files" -
This might get you on the right track:
Assembly assembly.LoadFrom("my_assembly.dll");
Type type=assembly.GetType("my_class");
MethodInfo mi=type.GetMethod("my_method");Using MethodInfo, you can inspect the security, return type, parameters, etc. Lots of fun. [edit]Oops--LoadFrom, not GetType[/edit] Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"I already understand this much of Reflection, but that doesn't help me... I need to dynamically create a method without ever actually seeing any method, only an
EventInfo
object or something that it publicly exposes a reference to. -
I already understand this much of Reflection, but that doesn't help me... I need to dynamically create a method without ever actually seeing any method, only an
EventInfo
object or something that it publicly exposes a reference to.I'm not really sure if this will help, but I use this to dynamically create a delegate:
public delegate object InterfacePoint(EventData eventData);
...
void RegisterInterfacePointByReflection(object instance, string fncName)
{
Type ipType=typeof(InterfacePoint);
InterfacePoint ip=null;
ip=Delegate.CreateDelegate(ipType, instance, fncName) as InterfacePoint;
}You have to have an instance of the object containing the event sink though. If you don't have an instance, you have to pass CreateDelegate a MethodInfo instead. Is THIS anything closer to what you need? If not, can you give me an example of what you have and where you want to go with it? (Gee, that sounds a lot like a Microsoft tag line). Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files" -
I'm not really sure if this will help, but I use this to dynamically create a delegate:
public delegate object InterfacePoint(EventData eventData);
...
void RegisterInterfacePointByReflection(object instance, string fncName)
{
Type ipType=typeof(InterfacePoint);
InterfacePoint ip=null;
ip=Delegate.CreateDelegate(ipType, instance, fncName) as InterfacePoint;
}You have to have an instance of the object containing the event sink though. If you don't have an instance, you have to pass CreateDelegate a MethodInfo instead. Is THIS anything closer to what you need? If not, can you give me an example of what you have and where you want to go with it? (Gee, that sounds a lot like a Microsoft tag line). Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"Alright... What I'm working on involves the dynamic creation of a wrapper class (through use of the Emit namespace) around a provided class that I see only as its Type object representation. As part of building the wrapper class, I need to emit methods for each event of the original class such that each method fits the requirements to be used in the delegate type used for the targeted event. Unfortunately, I can't seem to find any way to pull out anything that I could use to dynamically discover the required method signature for the creation of a method that can be used in the initialization of a provided delegate. The primary thing available to me with respect to the event is the EventInfo object, but the only thing that that exposes as to the event handler required for it is the Type object representing the class that inherits the System.Delegate class. This is a Type object like any other, and I can't find any way to pull the method signature out of it that it wants for its instantiation.
-
I'm working on the basis for a CodeProject article, but stuck on a couple of issues. The current problem is as follows: I have to dynamically create a method that can be used as an event handler for a particular event, given only the
EventInfo
object that represents the event. This includes a reference to the type (subclassed fromDelegate
and accessed through theEventInfo.EventHandlerType
property) that is created to represent the method in delegate form. However, there doesn't seem to be an obvious way to read out the desired method form (parameters & parameters' types, return type, etc.). Does anyone know how to get this method semi-signature out of the EventInfo, the event handlerType
, or anything else that's dynamically accessible through the framework (almost certainly through Reflection)? Thanks! Eric AstorI think this is what you are looking for :)
public delegate void SymEventHandler(Symantic type, ParserContext context)
{
public SymEventHandler(object object, IntPtr method);
public virtual IAsyncResult BeginInvoke(Symantic type, ParserContext context, AsyncCallback callback, object object);
public virtual void EndInvoke(IAsyncResult result);
public override void Invoke(Symantic type, ParserContext context);
}Lookup the Invoke method with Reflection.
-
I think this is what you are looking for :)
public delegate void SymEventHandler(Symantic type, ParserContext context)
{
public SymEventHandler(object object, IntPtr method);
public virtual IAsyncResult BeginInvoke(Symantic type, ParserContext context, AsyncCallback callback, object object);
public virtual void EndInvoke(IAsyncResult result);
public override void Invoke(Symantic type, ParserContext context);
}Lookup the Invoke method with Reflection.
Looking up the Invoke method with Reflection is exactly what I needed... I really appreciate your help.