c# - 使用 c# 库的 Powershell 无法识别具有不同返回类型的重载方法
问题描述
我的代码有两个函数,它们具有不同的返回类型和不同的参数化。一个接受一个字符串并返回一个布尔值。另一个接受一个字符串数组并返回一个字典。当我们从 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 可能出于某种原因被解释为字符串。
推荐阅读
- c# - win10/windos server2016上的注册服务奇怪的行为
- c# - Xunit 内联自动数据与自动夹具问题
- youtrack - 将以前的问题 ID 提取到 youtrack 报告中
- linux - 生成命令行“dbus-launch --autolaunch="xyz" --binary-syntax --close-stderr”时出错:子进程以代码1退出
- amazon-web-services - AWS - 通过 SNS 或其他机制通知桌面应用程序
- swift - 如何在 Swift 中从 UIView 导航推送视图控制器
- c# - .NET / C# 中的 HTTP GET 而不是 HTTP Connect
- css - 如何使用 React 和 CSS 内联标题和导航链接
- javascript - 如何将 Web 应用程序输入数据保存在同一个 Web 位置?
- python - 在 Python 中创建包含某些单词的文档列表