Subscribe to an event through COM GetEvent return null
-
Hi I'm having a source code of an Api, but since we don't have to change it. I'm creating a windows service that register it as a COM component then it could be used from the client. I'll try to simplify my issue. I started with the creation of an interface having the function and an event (that uses delegate)
[Guid(ApiServices.InterfaceId), ComVisible(true)]
public interface IApiServices
{
[DispId(1)]
ApiResult Function1();\[DispId(2)\] event TestEvent event1;
}
Then I created a class that implements this inteface
\[ Guid(ClassId), ComVisible(true) ClassInterface(ClassInterfaceType.None) ProgId("ApiService") \]
public class ImplApiServices : MarshalByRefObject, IApiServices
{
private ApiObject _apiObject;public ImplApiServices ()
{
_apiObject= new ApiObject();
_apiObject.TestEvent += OnEvent1;
}ApiResult Function1()
{
return _apiObject.Function1();
}public event TestEvent event1;
private void OnEvent1(object e)
{
if (event1 != null)
event1(e);
}
}And then I did all the other steps to register it as a COM component and launch use it through a service. when I call it using a client, it is recognized and the function returns the result perfectly
var _apiServiceObjType= Type.GetTypeFromProgID("ApiService");
var _apiServiceObj= Activator.CreateInstance(_apiServiceObjType);var result = (ApiResult )_apiServiceObjType.InvokeMember("Function1",
BindingFlags.InvokeMethod, null, _apiServiceObj, null);But when I want to subscribe a method to the event, I get a null eventInfo in the following instruction and can't go further
EventInfo eventInfo = _apiServiceObjType.GetEvent(event1);
Any link or recommendation that helps with this kind of problem is welcome. Thanks
-
Hi I'm having a source code of an Api, but since we don't have to change it. I'm creating a windows service that register it as a COM component then it could be used from the client. I'll try to simplify my issue. I started with the creation of an interface having the function and an event (that uses delegate)
[Guid(ApiServices.InterfaceId), ComVisible(true)]
public interface IApiServices
{
[DispId(1)]
ApiResult Function1();\[DispId(2)\] event TestEvent event1;
}
Then I created a class that implements this inteface
\[ Guid(ClassId), ComVisible(true) ClassInterface(ClassInterfaceType.None) ProgId("ApiService") \]
public class ImplApiServices : MarshalByRefObject, IApiServices
{
private ApiObject _apiObject;public ImplApiServices ()
{
_apiObject= new ApiObject();
_apiObject.TestEvent += OnEvent1;
}ApiResult Function1()
{
return _apiObject.Function1();
}public event TestEvent event1;
private void OnEvent1(object e)
{
if (event1 != null)
event1(e);
}
}And then I did all the other steps to register it as a COM component and launch use it through a service. when I call it using a client, it is recognized and the function returns the result perfectly
var _apiServiceObjType= Type.GetTypeFromProgID("ApiService");
var _apiServiceObj= Activator.CreateInstance(_apiServiceObjType);var result = (ApiResult )_apiServiceObjType.InvokeMember("Function1",
BindingFlags.InvokeMethod, null, _apiServiceObj, null);But when I want to subscribe a method to the event, I get a null eventInfo in the following instruction and can't go further
EventInfo eventInfo = _apiServiceObjType.GetEvent(event1);
Any link or recommendation that helps with this kind of problem is welcome. Thanks
I believe you need to use the GetEvent() with "binding flags":
Quote:
An event is considered public to reflection if it has at least one method or accessor that is public. Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.
[Type.GetEvent Method (String, BindingFlags) (System)](https://msdn.microsoft.com/en-us/library/w7k6k1z6(v=vs.110).aspx)
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
I believe you need to use the GetEvent() with "binding flags":
Quote:
An event is considered public to reflection if it has at least one method or accessor that is public. Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.
[Type.GetEvent Method (String, BindingFlags) (System)](https://msdn.microsoft.com/en-us/library/w7k6k1z6(v=vs.110).aspx)
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal