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?

Community
  • 1
  • 1
Wayne Phipps
  • 2,019
  • 6
  • 26
  • 31
  • 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? – Sergio Rosas Aug 08 '12 at 10:23
  • That's what I was doing, just wondering if there is a smarter approach which would perhaps be more dynamic – Wayne Phipps Aug 08 '12 at 10:29
  • Well, you could use Linq ICollection col = dbCollection.Select(x => new Person(x.Prop1, x.Prop2, ...); Or something similar – Sergio Rosas Aug 08 '12 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. – Wayne Phipps Aug 08 '12 at 11:01

2 Answers2

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.

Derek
  • 8,300
  • 12
  • 56
  • 88
  • 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. – Wayne Phipps Aug 08 '12 at 10:51
  • @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. – NH. Sep 27 '17 at 14:47
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; }
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • Many many thanks, you helped me greatly! Apologies for the delay in my reply, I was testing various ideas (yours included). – Wayne Phipps Aug 10 '12 at 14:02
  • 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 – Wayne Phipps Aug 10 '12 at 14:14
  • Sorry for this super late comment but i just have a quick question What type is Type.GetType("SO.Form2+Person") – Tj Laubscher Jun 19 '19 at 19:48
  • @TjLaubscher `namespace.class+subclass` https://learn.microsoft.com/en-us/dotnet/api/system.type.gettype?view=netframework-4.8 – L.B Jun 20 '19 at 20:58
  • 1
    @L.B thank you very much makes everything clear to me now :D – Tj Laubscher Jul 17 '19 at 05:25