简体   繁体   中英

Can't Access Public Method in base class

So I was putting together some simple unit tests for a console application I was writing. When I was putting the final touches on a test, I came across a strange access issue.

Assert.Equals(testRegex.Evaluate(testString),true);

This threw a error saying the Evaluate method was inaccessible due to its protection level. It is public in the base class. Here is the base class and sub class.

abstract class RegexEvaluator
{
    //Fields
    protected string Regex { get; set; }

    public bool Evaluate(string text)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(text, Regex))
            return true;
        else
            return false;
    }
}

public class SSNRegex : RegexEvaluator
{
    public SSNRegex()
    {
        //Stubbed for now, don't worry about the regex
        this.Regex = "\\d{3}-\\d{2}-\\d{4}";
    }
}

I was able to solve the issue by adding to the SSNRegex

public bool Evaluate(string text)
{
    return base.Evaluate(text);
}

My question is I'm confused about why I wasn't able to access the base class method. Why?

You must sign RegexEvaluator as public . It's internal by default, that's why you can't access it from another assembly (such as the unit test).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM