Search code examples
c#typesormlite-servicestack

How to clone a type T in c#?


I have a type Foo as follow:

[Alias("Boo")]
public class Foo
{
  public int Id { get; set; }
  public string Name { get; set; }
}

I am saving the history of changes of the objects properties, and for performance reasons I would like to create a second table where to push all history. Unfortunately OrmLite does not support (afaik) multiple tables per type. The alias will simply be used instead of the type name when defined.

Since I am using generic data controllers, I need to clone the actual type T (with a different name Boo). I do not need to convert/cast objects between both types, but it would be helpful if that is possible as well.


Solution

  • The solution turned to be easier than I though - although did not cross my mind at the beginning : Inheritance!

    As I do not have control over the Foo classes, I cannot just manually write a new type to reflect it (members may change in Foo). Creating a new type Boo that inherit from Foo with no extra members defined will allow both casting and table creation with a new name using OrmLite.

    public class Foo
    {
      public int Id { get; set; }
      public string Name { get; set; }
    }
    
    public class Boo : Foo
    {
    
    }