可以从PowerShell的传递null为.NET API,它需要一个字符串? [英] Possible to pass null from Powershell to a .Net API that expects a string?

查看:165
本文介绍了可以从PowerShell的传递null为.NET API,它需要一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
        }
    }
}

脚本:

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)

输出:


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

我在想什么?

What am I missing?

推荐答案

根据这一 MS连接发出时,这是一个已知的问题。有贴有一对夫妇的解决方法,也喜欢用反射传递paramaters(这是聪明的,但有点傻,它的需要)。干杯!

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!

这篇关于可以从PowerShell的传递null为.NET API,它需要一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆