How to automatically copy a property declaration to base class

249 Views Asked by At

I have a property A in all subclasses of base class Base.
How can I generate an abstract property definition of property A into base class Base?

I know ReSharper's refactoring Pull Members Up, but that moves the property to base class.

I need an abstract property in base class and a overriding properties in all sub classes. Is there a refactoring in Visual Studio or in ReSharper that can do it automatically for me?

3

There are 3 best solutions below

6
On BEST ANSWER

There is a checkbox "Make abstract" for that in ReSharper Pull Members Up dialog : enter image description here

2
On

I'm not sure Resharper can move up and create an abstraction as you want automatically, but you can atleast define it manually like this

In abstract class:

public abstract double A
{
    get;
}

In Sub class:

public override double A
{
    get
    {
        return 3.141;
    }
}

It might be a clearner design to define a new Interface (or use an existing one) and define the property in the interface. That way, your existing subclasses won't have to use override.

0
On
public interface IInterface {
    string MyProperty { get; }
}

public class Class : IInterface {
    public string MyProperty { get; set; }
}


public abstract class AbstractClass {
    public abstract string Value { get; }
}

public class ConcreteClass : AbstractClass {

    private string m_Value;
    public override string Value {
        get { return m_Value; }
    }

    public void SetValue(string value) {
        m_Value = value;
    }
}

I hope this will be helpful to you.