WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: ChuckC on May 28, 2021, 12:34:55 PM

Title: WIL CLR Support - How is "null" represented as a parameter in a method call?
Post by: ChuckC on May 28, 2021, 12:34:55 PM
With WIL's CLR Support, how is "null" represented as a parameter in a method call?

If a method is overloaded where the overloads have the same number of parameters and the parameters in the same positions in the signature are also reference types, how is type casting performed to ensure that correct overload resolution is performed:

void Method(int Value1, object Ref1) {...}

void Method(int Value1, string Ref1) {...}

void Method(int Value1, MyClass Ref1) {...}


Assuming that I want to call the method with the 2nd parameter being null, for overload resolution to be successful, I either need to be able to cast 'null' to the correct type for the "Ref1" parameter, or I need to be able to create a variable of the appropriate .NET type [string, object or MyClass] which is a null reference.

Title: Re: WIL CLR Support - How is "null" represented as a parameter in a method call?
Post by: td on May 28, 2021, 01:58:58 PM
That is tough to answer because under the hood .Net has different representations of "Null" depending on the class and/or namespace using it.  You could try
Code (winbatch) Select
objNullable = ObjectClrNew('System.Nullable')
exit

but I have no idea if that will work in any given instance. The problem with the class is it doesn't have any underlying type so any of the three allowed operations on the Nullable class will likely fire an exception. 

Some classes are automagically nullable like System.String for example. You could try casting something like this
Code (winbatch) Select
objWhoKnows = ObjectClrType('System.String', 0)

but again, I have no idea if or when it will work. However, I have had success with the second example in at least one instance.

Of course, there is always the C# compiler workaround.
Title: Re: WIL CLR Support - How is "null" represented as a parameter in a method call?
Post by: td on May 28, 2021, 03:03:47 PM
The "SystemNullable" class constructor does accept a parameter but I have no idea how useful the resulting object might be.

Code (winbatch) Select
AnInt = ObjectClrNew('System.Int32', 01)
NullableInt = ObjectClrNew('System.Nullable', AnInt)

or just
Code (winbatch) Select
NullableInt = ObjectClrNew('System.Nullable', 0)
Title: Re: WIL CLR Support - How is "null" represented as a parameter in a method call?
Post by: td on May 29, 2021, 05:04:43 PM
Another random idea. To create a null reference to a class perhaps cast the following to the desired class using ObjectClrType:
Code (winbatch) Select
refNull = ObjectType('dispatch', 0)