Ye Myat Aung
Ye Myat Aung

Reputation: 1853

How to create objects dynamically with C#?

I'm trying to create objects dynamically but I don't know how to. What I need is, I have a class for that object, and objects properties are stored in the database. Then I'll need to compare the properties of each object to get the desired result.

So I need to dynamically create objects on the fly with the properties loaded from database.

Upvotes: 1

Views: 10866

Answers (9)

Robert Harvey
Robert Harvey

Reputation: 180787

Rob Conery did a small project called Massive (now maintained by FransBouma) that pretty much does what you're trying to accomplish. It's essentially a small ORM, in 400 lines of Dynamic C# 4.0 code.

Rob has been doing this kind of thing for quite some time with SubSonic, so you might find his approach with Massive quite interesting.

https://github.com/FransBouma/Massive

Upvotes: 0

Mike Webb
Mike Webb

Reputation: 8993

I have been working on something similar to this. There are several things:

  1. Include the System.Reflection namespace
  2. Create an object dynamically using Activator
  3. Get the object properties using the myObjectType.GetProperties() method

Here is an example of a generic object creation function using the above methods:

using System.Reflection;

public static Item CreateItem<Item>(object[] constructorArgs, object[] propertyVals)
{
    //Get the object type
    Type t = typeof(Item);

    //Create object instance
    Item myItem = (Item)Activator.CreateInstance(t, constructorArgs);

    //Get and fill the properties
    PropertyInfo[] pInfoArr = t.GetProperties();
    for (int i = 0; i < pInfoArr.Length; ++i)
        pInfo.SetValue(myItem, propertyVals[i], null); //The last argument is for indexed properties

    return myItem;
}

Of course the above example assumes that the values in the property value array are arranged correctly, which is not necessarily the case, but you get the idea.

With the PropertyInfo class you can get properties, get property names, get attributes associated with the properties, etc. Powerful technology. You should be able to do what you need with the above info, but if not let me know and I will add more info.

Upvotes: 1

Robb
Robb

Reputation: 3851

If you have a number of objects you want to instantiate from database values it can be done something like this.

//database code goes here, results go in results
List<ClassName> l = new List<ClassName>()
foreach(Row r in results){
   l.Add(new ClassName(){ClassProperty1 = r.Property1,ClassProperty2 = r.Property2});
}

Upvotes: 0

iBrAaAa
iBrAaAa

Reputation: 394

I think that you want to retrieve rows from the DB and directly assign them to object given that the properties of the object are equivalent to the columns of DB table. If that what you mean then I believe you can't :)

Upvotes: 0

MattStacey
MattStacey

Reputation: 895

You could use reflection to dynamically build your objects:

Reflection msdn reference

Upvotes: 0

Howard
Howard

Reputation: 3848

Check this class, compile in the realtime. But it's performance is not quite good. http://msdn.microsoft.com/zh-cn/library/microsoft.csharp.csharpcodeprovider(VS.80).aspx

Upvotes: 0

Beth
Beth

Reputation: 9607

I don't think you need to create objects dynamically, just create one statically that matches your db schema with the property details, then you can compare the values of the properties across rows, or within an instance of your object.

Upvotes: 0

asawyer
asawyer

Reputation: 17808

Maybe Activator is what your looking for?

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

Upvotes: 0

Blindy
Blindy

Reputation: 67362

Are you talking about Dictionary?

var dict=new Dictionary<string, string>();
dict.Add("property1", "val1");
dict.Add("property2", "val2");

var prop2val=dict["property2"];

Upvotes: 0

Related Questions