Create Instance of DotNet variables – the hard way!

Have you ever seen this error:

Assignment is not allowed for this variable? But it’s just a DotNet class that I’m trying to make an instance of. (The class in my example is DocumentFormat.OpenXml.Spreadsheet.Text that is used for adding strings to Excel sheets).

But this is Applied Hacking, so we can use reflection to get around this. Here is “DotNetConstructor”, simplified to a version where the constructor takes just one parameter

The idea is simple, get the ConstructorInfo and Invoke it. To do that, you need a DotNet Array first with the Types of parameters and then antoher array with the parameter values. This function can easily be extended to handle multiple parameters.

So instead of using:

Text := Text.Text('Value');

We can just do:

DotNetConstructor(Text,'Value',Text);

Enjoy 🙂

Share thisShare on FacebookTweet about this on TwitterShare on RedditShare on Google+Share on LinkedIn

Leave a Reply