我的代码具有两个函数,它们具有不同的返回类型和不同的参数化。一个取一个字符串并返回布尔值。另一个接受字符串数组并返回字典。当我们从c#.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”。
答案 0 :(得分:1)
您是否尝试过将变量转换为字符串数组?
Import-Module .\DotNetForPowershell.dll
$ArrayOfEmails = ("test@test.com", "@anotheremail.com", "em.ail@test.com")
[DotNetForPowershell.Utils.StingTests]::ValidateEmailFormat([System.String[]]$ArrayOfEmails)
出于某种原因,PSObject可能被解释为字符串。