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.

3
  • 1
    See stackoverflow.com/questions/646258/… and stackoverflow.com/questions/6166236/add-properties-at-runtime and stackoverflow.com/questions/1653046/… - it's already pretty well covered on StackOverdlow Commented Jun 11, 2012 at 9: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? Commented Jun 11, 2012 at 9: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. Commented Jun 11, 2012 at 10:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This could be a good option. Do you know how to add attributes to the member vars?
0

It is possible in C# 4.0 using dynamic types.

3 Comments

Isn't it possible using Reflection and TypeBuilder class?
A little more information would be really welcomed. What about the ExpandoObject?.
@Notbad indeed, ExpandoObject is a handy and common implementation of dynamic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.