WIL CLR Support - How is "null" represented as a parameter in a method call?

Started by ChuckC, May 28, 2021, 12:34:55 PM

Previous topic - Next topic

ChuckC

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.


td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade