Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to pass null from Powershell to a .Net API that expects a string?

API:

namespace ClassLibrary1
{
    public class Class1
    {
        public static string Test(string input)
        {
            if (input == null)
                return "It's null";
            if (input == string.Empty)
                return "It's empty";
            else
                return "Non-empty string of length " + input.Length;
        }
    }
}

Script:

add-type -path C:\temp\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll
[classlibrary1.class1]::Test($null)
[classlibrary1.class1]::Test([object]$null)
[classlibrary1.class1]::Test([psobject]$null)
[classlibrary1.class1]::Test($dummyVar)
[classlibrary1.class1]::Test($profile.dummyProperty)

Output:

It's empty
It's empty
It's empty
It's empty
It's empty

What am I missing?

like image 558
Richard Berg Avatar asked Jan 04 '10 21:01

Richard Berg


2 Answers

In order to pass a null value to an API call, use [NullString]::Value.

like image 184
Bob M Avatar answered Sep 20 '22 14:09

Bob M


According to this MS connect issue, this is a known problem. There are a couple workarounds posted there, too, like using reflection to pass the paramaters (which is clever, but kinda silly that it's required). Cheers!

like image 38
Scott Anderson Avatar answered Sep 20 '22 14:09

Scott Anderson