Great article, thanks! And a pretty cool way to explain things in LV terms
OK, here the detailed explanation as promised:
You can find a very simple example in the ZIP file attached to my first post, GZipStream.Example.vi . There's a file Hello.txt, if you run GZipStream.Example.vi you'll get the compressed file Hello.txt.gz . To decompress it, activate the other frame of the diagram disable structure and run it. The output will be Hello2.txt . There's not much to say about this example VI, the code consists of just .NET invoke nodes and properties.
The code of CreateGZIPStream.vi is that what matters here. We use .NET reflection to call a constructor that would be otherwise inaccessable for LV. Reflection is the ability to query a .NET assembly (DLL or EXE) for the classes (and other types) contained within and ask these classes for their properties, methods, events and constructors. It is built into the .NET framework and thus always at your service if the .NET support of LV isn't enough. The most important .NET data type when using reflection is System.Type .
OK, let's now dissect the code. Sorry about using C# here but I know no better way to explain .NET code.
This is what we want to do:
GZipStream CreateGZipStream(Stream stream, CompressionMode compressionMode) {
// Call the constructor.
return new GZipStream(stream, compressionMode);
}
Since LV doesn't allow us to select this constructor we need another way to get and invoke it. Reflection makes that possible, although the code becomes much more complex. CreateGZipStream.vi is roughly equivalent to the following C# code:
GZipStream CreateGZipStream(Stream stream, CompressionMode compressionMode) {
// Get the types.
Type streamType = typeof(Stream);
Type compressionModeType = typeof(CompressionMode);
Type gzipStreamType = typeof(GZipStream);
// To get the desired constructor we need its argument types and bundle them into an array.
Type[] constructorArgTypes = new Type[] { streamType, compressionModeType };
// Get the constructor.
ConstructorInfo gzipStreamConstructor = gzipStreamType.GetConstructor(constructorArgTypes);
// Bundle the constructor arguments into an array.
object[] constructorArgs =new object[] { stream, compressionMode };
// Call the constructor and cast the result.
return gzipStreamConstructor.Invoke(constructorArgs) as GZipStream;
}
Unfortunately LV complicates things a little further: We have no direct equivalent of typeof(). We can work around that by either using GetType() or loading the type from the assembly by name. Using GetType() is easier but it requires a valid .NET reference. Thus we need both:
GZipStream CreateGZipStream(Stream stream, CompressionMode compressionMode) {
// Get the types.
Type streamType = stream.GetType();
Type compressionModeType = compressionMode.GetType();
Type gzipStreamType = compressionModeType.Assembly.GetType("System.IO.Compression.GZIPStream");
// To get the desired constructor we need its argument types and bundle them into an array.
Type[] constructorArgTypes =new Type[] { streamType, compressionModeType };
// Get the constructor.
ConstructorInfo gzipStreamConstructor = gzipStreamType.GetConstructor(constructorArgTypes);
// Bundle the constructor arguments into an array.
object[] constructorArgs =new object[] { stream, compressionMode };
// Call the constructor and cast the result.
return gzipStreamConstructor.Invoke(constructorArgs) as GZipStream;
}
But that's still not enough. For LV CompressionMode is an enum, in .NET an enum is a full featured object. So we have also to construct a CompressionMode .NET object in LV and set its enum value. There's no direct equivalent to the 'value__' property in C#, I guess LV does something special here. Now we're ready to invoke the constructor in LV. The snippet again:
We construct the CompressionMode object and use its Assembly property to get the GZIPStream Type object. Then we ask for the constructor using the parameter types bundled into an array. Finally we call that constructor with the arguments bundled into an array and cast the result to GZipStream.
A note about the style of the code: When it comes to external reference-based code I use a single error wire through all nodes to enforce some kind of control flow. Furthermore it's important to close all open .NET references after using them.
I'm using .NET reflection to do things that are not supported by LV for a couple of years now, in my case .NET Remoting with events, see here (no reflective constructor invocation, though).