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

后端 未结 3 1219
醉梦人生
醉梦人生 2020-12-16 16:50

API:

namespace ClassLibrary1
{
    public class Class1
    {
        public static string Test(string input)
        {
            if (input == null)
                


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-16 17:06

    this is just how PowerShell behaves - it will always try to convert an object as long as it is convertible to the target type (in this case string). PowerShell will always convert null (the absence of a value) to String.Empty when casting into a string object.

    Take a look at Bruce Payette's book "Windows PowerShell in Action", around page 142. Bruce is one of the architects behind PowerShell.

    It's kinda one of those documented little gotchas of the scripting language, and we should definitely be aware of it.

提交回复
热议问题