2

I've been thinking about creating objects dynamically using data stored in a database. I've done something similar in the past by serializing all the properties of each object but that was a few years ago and times have changed.

I actually like the method outlined in the answer to the following question: How to do dynamic object creation and method invocation in .NET 3.5

I'm wondering if it's possible to take this one step further and set all the properties of the object using stored data.

Obviously I could set each of the properties individually IE:
Person p = new Person();
p.Name = "Me";
p.Age = 31;

Would it be possible to do something more in-line (sorry don't quite know what this is called) like Person p = new Person() { Name = "Me", Age = 31 }; where the Name = "Me", Age = 31 properties or string could originate from a database??

Any thoughts?

4
  • Once you have retrieved the data from DB and stored someway into a collection, why don't you simply iterate over this collection and create the objects? Commented Aug 8, 2012 at 10:23
  • That's what I was doing, just wondering if there is a smarter approach which would perhaps be more dynamic Commented Aug 8, 2012 at 10:29
  • Well, you could use Linq ICollection<Person> col = dbCollection.Select(x => new Person(x.Prop1, x.Prop2, ...); Or something similar Commented Aug 8, 2012 at 10:32
  • I like the idea of Linq but unless I'm wrong, don't think I can apply it in these circumstances. The problem is that the table is likely to contain data relating to different objects/components. Commented Aug 8, 2012 at 11:01

2 Answers 2

2

Have you thought about using LINQ to SQL? It takes all the tables of your SQL database and converts them to c# classes. You access all your data via a DataContext object, each table becomes a class you can call / edit / Update via LINQ.

Read up on the following material. There are many examples out there on the net.

http://msdn.microsoft.com/en-us/library/bb425822.aspx

I hope this helps.

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

2 Comments

I can see that Linq will create classes from my tables but don't know if it will help as different objects have been persisted to different records within the same table. As a very over-simplified example, one value within a record may be 'objectType' which would then indicate the type of object stored in the 'objects' table.
@WaynePhipps, that isn't generally a good way to make SQL tables, but if the database is out of your control, you could still use the auto-generated type from LINQ as the parent class, and then just define classes that inherit from it for each of the types stored in the table. If you do have control over the db, each type gets (at least) one table.
1

Assuming you read the values from DB and put in a dictionary like:

Dictionary<string, string> parameters = new Dictionary<string, string>()
{
    {"Name","Scott"},
    {"Age","30"}
};

(See the dictionary's type: string, string But Age is int in fact)

You can dynamically create your object as

var obj = Activator.CreateInstance(Type.GetType("SO.Form2+Person"));

foreach (var pi in obj.GetType().GetProperties())
{
    pi.SetValue(obj, Convert.ChangeType(parameters[pi.Name], pi.PropertyType));
}

--

public class Person
{
    public string Name { set; get; }
    public int  Age { set; get; }
}

5 Comments

Many many thanks, you helped me greatly! Apologies for the delay in my reply, I was testing various ideas (yours included).
Because I was mostly dealing with UI controls, I actually ended up using the TypeDescriptor.GetProperties method itterating through the properties and updating any contained within the dictionary. @L.B your an asset to the community! Many thanks
Sorry for this super late comment but i just have a quick question What type is Type.GetType("SO.Form2+Person")
@TjLaubscher namespace.class+subclass learn.microsoft.com/en-us/dotnet/api/…
@L.B thank you very much makes everything clear to me now :D

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.