开发者

How to create objects dynamically with C#?

开发者_JAVA百科

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.


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.


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.


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});
}


Are you talking about Dictionary?

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

var prop2val=dict["property2"];


Maybe Activator is what your looking for?

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


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


You could use reflection to dynamically build your objects:

Reflection msdn reference


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 :)


Rob Conery did a small project called Massive 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.

http://blog.wekeroad.com/helpy-stuff/and-i-shall-call-it-massive

Some of the code is explained here, with examples:

http://blog.wekeroad.com/microsoft/the-super-dynamic-massive-freakshow

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜