How to remove duplicate matching? There are my shapes (for example):

public abstract class Shape
{
  protected int _id;
  protected string _description;

  public abstract string ToXml();
}

public sealed class Triangle : Shape
{
  public int TriangleId { get { return _id; } set { _id = value; } }
  public string TriangleDescription { get { return _description; } set { _description = value; } }
  public ConsoleColor color { get; set; }

  public override string ToXml()
  {
    // First implementation
    throw new NotImplementedException();
  }
}

public sealed class Circle : Shape
{
  public int CircleId { get { return _id; } set { _id = value; } }
  public string CircleDescription { get { return _description; } set { _description = value; } }
  public int Radius { get; set; }

  public override string ToXml()
  {
    // Second implementation
    throw new NotImplementedException();
  }
}

public sealed class Square : Shape
{
  public int SquareId { get { return _id; } set { _id = value; } }
  public string SquareDescription { get { return _description; } set { _description = value; } }
  public int Edge { get; set; }

  public override string ToXml()
  {
    // Third implementation
    throw new NotImplementedException();
  }
}


Client have to choose one kind of shape to get xml-view:

public enum ShapeType
{
  Triangle,
  Circle,
  Square
}

public class Module
{
  public string GetXml( ShapeType type, int id )
  {
    switch( type )
    {
      case ShapeType.Circle:
        return DB.GetEntity( id ).ToTypedEntity&lt;Circle&gt;().ToXml();
      case ShapeType.Square:
        return DB.GetEntity( id ).ToTypedEntity&lt;Square&gt;().ToXml();
      case ShapeType.Triangle:
        return DB.GetEntity( id ).ToTypedEntity&lt;Triangle&gt;().ToXml();
    }
  }
}


As you can see, there are too much the same code parts that are matching enumerations with shape's types. It is not good.

How can I avoid it?
Just expose the class types to the user (which they already appear to be) rather than forcing them to use a gawd-awful enum.

public class Module
{
  public string GetXml&lt;TShape&gt;( int id ) where TShape : Shape
  {    
    return DB.GetEntity( id ).ToTypedEntity&lt;TShape&gt;().ToXml();    
  }
}


The the user can just make a normal call like

new Module().GetXml&lt;Triangle&gt;(34);