0

I'm using an ORM sytem (sqlite-net) for my android application. I want to be able to create my DB mapping classes dynamically, and be able to add attributes to members, is this possible? I would need to create something like this dynamically:

class ConfigData
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }
    [MaxLength(20)]
    public string ip { get; set; }
    [MaxLength(128)]
    public string port { get; set; }
}

Thanks in advance.

Notbad
  • 5,936
  • 12
  • 54
  • 100
  • 1
    See http://stackoverflow.com/questions/646258/how-can-i-add-dynamically-field-to-the-class-in-c-sharp and http://stackoverflow.com/questions/6166236/add-properties-at-runtime and http://stackoverflow.com/questions/1653046/what-are-the-true-benefits-of-expandoobject - it's already pretty well covered on StackOverdlow – dash Jun 11 '12 at 09:20
  • 2
    It really depends what is going to be consuming the object. Different APIs support different models. Sometimes you just have reflection; sometimes you have ComponentModel; sometimes you might be able to use `dynamic`. Really really depends on who is going to be consuming it. The "reflection only" is the "works most places" option, but requires things like `TypeBuilder`. Also, though: what is the problem defining `ConfigData` at compile time? – Marc Gravell Jun 11 '12 at 09:25
  • The problem here, is that I'm trying to create a little version converter between different table structures. For example I would like to know how the field ip in ConfigData version 10 was named in version 5 (Because I need to export to previous version). I need this to provide data from a table version to another below. This is why I wanted to define ConfigData dynamically depending on the version I want to get. Anyway I'm considering now using class/member attributes and see if I can accomplish this in an easier way. – Notbad Jun 11 '12 at 10:00

2 Answers2

1

Maybe the Reflection namespace is what you might use, here's an example: http://mironabramson.com/blog/post/2008/06/Create-you-own-new-Type-and-use-it-on-run-time-(C).aspx

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Ivan Golović
  • 8,732
  • 3
  • 25
  • 31
0

It is possible in C# 4.0 using dynamic types.

Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50