1

My code has two functions with different return types and different parameterization. One takes a single string and returns a bool. The other takes a array of strings and returns a dictionary. When we run the library from a c# .Net console application it recognizes the overload and selects for the proper return type. When we import the module containing the dll into Powershell and pass the function a string, we get the boolean return type as expected. However, when we pass an array of strings, we still get the boolean return type as if it's not detecting the overloaded method. Yet, we don't get any errors for passing the function the wrong type of parameter, we just get a return of "false".

We've tried type casting the array that we are passing as well as casting the return type to a dictionary. We also tested regular overloading with the same return types and it worked fine.

// C# Code

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 Code

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

Expected: Passing an array of strings returns dictionary object

Actual: Passing an array of string returns "false".

1
  • Where is StoredRegEx.StoredExpressions come from? Commented Jan 11, 2019 at 3:49

1 Answer 1

1

Have you tried casting the variable as a string array?

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

The PSObject may be interpreted as a string for some reason.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, that worked for me. Array of object seems to be the cause of the issue.
This is the correct answer. Tested and it works great. It looks like when you don't type cast the input array, you are actually inputting an array of PSObjects. For anyone wondering, either the input or return can be type casted to achieve these results (we tested both). Thanks for your help @Paul G!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.