Question

I have a library which has shipped (internally, but still shipped), and I would like to rename a public class within the library.

What I would like is a way to make sure that

  • client code keeps on working
  • client code gets a deprecation warning on compilation on use of the class name

  • Is there any way to alias the class in some way, which will exhibit the above properties?

  • Is there any way to avoid this problem in the future (other than making sure classes are named and spelled properly?) Note that I would like to keep a public constructor, so working with interfaces will not fix all my problems.
Was it helpful?

Solution

You can' provide any public alias name to the class, but you can forward all public properties, methods, etc. to a private instance of the renamed class and decorate the class with the obsolete attribute:

[Obsolete("Please use LatestGreatest instead.")]
public class OldSchool
{
    private LatestGreatest _Target;

    public OldSchool()
    {
        _Target = new LatestGreatest();
    }

    public void DoSomething()
    {
        _Target.DoSomething();
    }

    [Obsolete("Please use LatestGreatest.DoItInSomeOtherWay()")]
    public void DoTheOldWay()
    {
        _Target.DoItInSomeOtherWay();
    }
}

public class LatestGreatest
{
    public void DoSomething()
    {
        Console.WriteLine("I'm so fresh and cool.");
    }

    public void DoItInSomeOtherWay()
    {
        Console.WriteLine("Let's do it...");
    }
}

Depending on the size of the public side of your old class this work can be quite tedious, but there is nothing else you can do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top