使用 c# 库的 Powershell 无法识别具有不同 return 类型的重载方法
Powershell using c# library not recognizing overloaded methods with different return types
我的代码有两个具有不同 return 类型和不同参数化的函数。一个接受一个字符串和 returns 一个布尔值。另一个采用字符串数组和 returns 字典。当我们 运行 来自 c# .Net 控制台应用程序的库时,它会识别重载并选择正确的 return 类型。当我们将包含 dll 的模块导入 Powershell 并向函数传递一个字符串时,我们得到了预期的 boolean return 类型。但是,当我们传递一个字符串数组时,我们仍然得到 boolean return 类型,就好像它没有检测到重载方法一样。然而,我们没有因为将错误类型的参数传递给函数而得到任何错误,我们只是得到 "false" 的 return。
我们已经尝试对传递的数组进行类型转换,并将 return 类型转换为字典。我们还使用相同的 return 类型测试了常规重载,并且效果很好。
// 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)
预期:传递字符串数组 returns 字典对象
实际:传递字符串数组 returns "false".
您是否尝试过将变量转换为字符串数组?
Import-Module .\DotNetForPowershell.dll
$ArrayOfEmails = ("test@test.com", "@anotheremail.com", "em.ail@test.com")
[DotNetForPowershell.Utils.StingTests]::ValidateEmailFormat([System.String[]]$ArrayOfEmails)
出于某种原因,PSObject 可能被解释为字符串。
我的代码有两个具有不同 return 类型和不同参数化的函数。一个接受一个字符串和 returns 一个布尔值。另一个采用字符串数组和 returns 字典。当我们 运行 来自 c# .Net 控制台应用程序的库时,它会识别重载并选择正确的 return 类型。当我们将包含 dll 的模块导入 Powershell 并向函数传递一个字符串时,我们得到了预期的 boolean return 类型。但是,当我们传递一个字符串数组时,我们仍然得到 boolean return 类型,就好像它没有检测到重载方法一样。然而,我们没有因为将错误类型的参数传递给函数而得到任何错误,我们只是得到 "false" 的 return。
我们已经尝试对传递的数组进行类型转换,并将 return 类型转换为字典。我们还使用相同的 return 类型测试了常规重载,并且效果很好。
// 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)
预期:传递字符串数组 returns 字典对象
实际:传递字符串数组 returns "false".
您是否尝试过将变量转换为字符串数组?
Import-Module .\DotNetForPowershell.dll
$ArrayOfEmails = ("test@test.com", "@anotheremail.com", "em.ail@test.com")
[DotNetForPowershell.Utils.StingTests]::ValidateEmailFormat([System.String[]]$ArrayOfEmails)
出于某种原因,PSObject 可能被解释为字符串。