Alan
Alan

Reputation: 3002

C# Access a static method that is hidden by a local property

I'm sure this must have been asked already, but I can't seem to find the answer. I need to know how to access a static method, when the class it is defined within has been hidden by an instance method with the same name.

I have a class which exposes a static method as follows:

public class Plan
{
    public static Plan Generate(Project project)
    {
        var plan = new Plan();
        // ... 
        return plan;
    }
}

I then have another class which contains a method called "Plan", from which I want to call the static method mentioned above:

public ActionResult Plan(int id)
{
    // ...
    var plan = Plan.Generate(project);
    // ...
}

The problem is that the class name 'Plan' is hidden by the method name, so I cannot call the static method directly.

Upvotes: 1

Views: 106

Answers (1)

ta.speot.is
ta.speot.is

Reputation: 27214

Qualify your access to the Plan type with the type's name. For example:

YourNamespace.Plan.Generate

That said, static methods are bad mkay. Make yourself an IPlanFactory, bind PlanFactory to it and let dependency injection do the rest (assuming you're using constructor injection and not that hairbrained dependency resolver stuff). Now it's unambigiously _planFactory.Generate(...) and you've just increased testability. Give yourself a raise!

Upvotes: 3

Related Questions