簡體   English   中英

使用 c# 庫的 Powershell 無法識別具有不同返回類型的重載方法

[英]Powershell using c# library not recognizing overloaded methods with different return types

我的代碼有兩個具有不同返回類型和不同參數化的函數。 一個接受單個字符串並返回一個布爾值。 另一個接受一個字符串數組並返回一個字典。 當我們從 ac# .Net 控制台應用程序運行庫時,它會識別重載並選擇正確的返回類型。 當我們將包含 dll 的模塊導入 Powershell 並向函數傳遞一個字符串時,我們得到了預期的布爾返回類型。 然而,當我們傳遞一個字符串數組時,我們仍然得到布爾返回類型,就好像它沒有檢測到重載的方法一樣。 然而,我們不會因為將錯誤類型的參數傳遞給函數而得到任何錯誤,我們只會得到“false”的返回值。

我們已經嘗試對傳遞的數組進行類型轉換以及將返回類型轉換為字典。 我們還使用相同的返回類型測試了常規重載,它運行良好。

// C# 代碼

public static class StringTests
{
    /// <summary>
    /// Test a string for valid email format
    /// </summary>
    /// <param name="email">email string to test</param>
    /// <returns>true if valid email format</returns>

    public static bool ValidateEmailFormat(string email)
    {
        Dictionary<string, string> exp = StoredRegEx.StoredExpressions;

        // Fail if two periods in a row
        if (Regex.IsMatch(email, exp["DoublePeriod"]))
        {
            return false;
        }

        // Fail if leading character is a period
        if (Regex.IsMatch(email, exp["LeadPeriod"]))
        {
            return false;
        }

        // Splitting email string around '@' delimeter.  We can test the results to check for multiple @'s, or @'s in the wrong location
        string[] splitEmail = email.Split('@');

        // Fail if anything other than exactly one '@' symbol in string.  If there is one '@' symbol located in email string that is not either the first 
        // or last character, then we should always get a string array with a length of two when we split.
        if (splitEmail.Length != 2)
        {
            return false;
        }

        // Fail if local string does not match local format
        if (!Regex.IsMatch(splitEmail[0], exp["LocalFormat"]))
        {
            return false;
        }

        // Fail if domain string is longer than 255 chars
        if (splitEmail[1].Length > 255)
        {
            return false;
        }

        // Fail if domain string begins or ends with a hyphen               TODO: Research if its exclusively hyphen because like dollar signs and percetages probably don't work
        if (splitEmail[1].StartsWith("-") || splitEmail[1].EndsWith("-"))
        {
            return false;
        }

        // Turn the domain string into subdomains around a '.' delimeter to check if any of the subdomains 
        string[] subDomains = splitEmail[1].Split('.');

        foreach (string subDomain in subDomains)
        {
            if (subDomain.Length > 63)
            {
                return false;
            }
        }

        // Fail if domain does not match domain format
        if(!Regex.IsMatch(splitEmail[1], exp["DomainFormat"]))
        {
            return false;
        }

        return true;
    }

    /// <summary>                                                                                                                                   // currently the overloaded dictionary return type is not working with powershell
    /// Overload takes an array of email strings and return dictionary with bool validation
    /// </summary>
    /// <param name="emails"></param>
    /// <returns></returns>
    public static Dictionary<string, bool> ValidateEmailFormat(string[] emails)
    {
        Dictionary<string, bool> validatedEmails = new Dictionary<string, bool>();

        foreach(string email in emails)
        {
            bool emailValid = ValidateEmailFormat(email);

            validatedEmails.Add(email, emailValid);
        }

        return validatedEmails;
    }

// Powershell 代碼

Import-Module .\DotNetForPowershell.dll
$ArrayOfEmails = ("test@test.com", "@anotheremail.com", "em.ail@test.com" 
[DotNetForPowershell.Utils.StingTests]::ValidateEmailFormat($ArrayOfEmails)

預期:傳遞字符串數組返回字典對象

實際:傳遞字符串數組返回“false”。

您是否嘗試將變量轉換為字符串數組?

Import-Module .\DotNetForPowershell.dll
$ArrayOfEmails = ("test@test.com", "@anotheremail.com", "em.ail@test.com")
[DotNetForPowershell.Utils.StingTests]::ValidateEmailFormat([System.String[]]$ArrayOfEmails)

由於某種原因,PSObject 可能會被解釋為字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM