vendredi 31 juillet 2015

Regex in Custom Configuration Partially Working

I have a ConfigurationProperty that is annotated with a RegexStringValidator that looks like this:

public class Test : ConfigurationElement
{
    [ConfigurationProperty("field", IsKey = true)]
    [RegexStringValidator("issuer|subject")]
    public string Field
    {
        get { return (string)base["field"]; }
    }
}

It always throws an exception saying that the RegexStringValidator failed. I can achieve success if I change the regex value to either of these:

  • "[a-zA-Z]*"
  • ".*"

The actual "field" value being validated is the simple string "issuer". I can see no reason why there should be a problem. In fact, the source code for RegexStringValidator does nothing fancy; it simply uses a Regex and checks for success.

So I tried to do the regex directly as a test, the same way that it is done inside RegexStringValidator:

Regex reg = new Regex(@"issuer|subject", RegexOptions.Compiled);
bool match = reg.Match("issuer").Success;

The above works as expected; it returns Success==true. I even used Telerik's "Just Decompile" to verify the System.Configuration source code indeed matches what is published online.

Again, I can see no reason why my "issuer|subject" RegexStringValidator regex constantly fails.

Any ideas?

The only clue I've turned up is when I tried the regex "[a-zA-Z]". Strangely, this failed because it did not have the trailing asterisk. So perhaps the regex must be written to match the full-length of the input. To test that theory, I changed my regex to "^issuer$|^subject$", but that still didn't work when applied to the RegexStringValidator. It does, however, work with my explicitly written regex test.

What makes the above anomaly interesting is that the source code for RegexStringValidator gives no reason of why "[a-zA-Z]" would fail. Does this imply that the code I'm reflecting and seeing online does not match what is actually executing on my machine? That doesn't seem plausible. So this is weird indeed...

I'm using a .net 4.0 build, on a machine that has .net 4.5 installed.

Aucun commentaire:

Enregistrer un commentaire