LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
G_diesel1990

Instantiate VIs as generic delegates when working with C# libraries

Status: New

When integrating libraries written in C# a common scenario that occurs is the necessity to cast a method to a generic delegate defined in the class in order to, for instance, subscribe callbacks to events belonging to sub-classes inherited by the object instantiated by the Constructor Node.

 

So far LabVIEW efficiently allows to subscribe to events, registering a VI as callback in case the event involved is fired, but does not allow to cast a VI to a generic delegate and that's very limiting.

 

The only workaround existing is to wrap the C# library, adding an additional C# layer that instantiates the delegates on behalf of LabVIEW and then exposes plain methods, properties and events that are straightly callable by LabVIEW itself.

 

The idea is to allow  LabVIEW to instantiate generic delegates based on the definition of the delegate provided by the C# library and return the delegate object so that it can be provided as input to library's methods requesting it.

 

This feature would make the LabVIEW integration of C# dlls more solid and completely manageable within LabVIEW code without further wrappers needed.

2 Comments
Intaris
Proven Zealot

"but does not allow to cast a VI to a generic delegate"

 

I'm not quite sure what you mean by this. Can you elaborate?

G_diesel1990
Member

Of course, I report here below a very trivial example that I hope will make the idea clearer:

 

Class A

 
public delegate uint ExampleCallback(uint A, string B, UInt16 C);
public event ExampleCallback ExampleEvent;
public void AddCallback(ExampleCallback callback)
{
ExampleEvent+= callback;
}
 
Class B
ObdFromOtherClass = Class A
ObdFromOtherClass.AddCallback((ExampleCallback)HandleEvent);
 

public uint HandleEvent((uint A, string B, UInt16 C)
{
#code here to handle the event

}

 
The line of pseudo-code I reported in bold is what I'd like LabVIEW to do with the change proposed.
Supposing that the DLL exposes directly only Class B, if HandleEvent was a VI, there is no way to cast it to ExampleCallback generic delegate and pass it to AddCallBack method. 
The change should allow to work on generic delegates the same ways as Register Event Callback already works for events that belong to the same class of the object you are directly instantiating (CLASS B in the example)
 
I hope I made myself clearer