开发者

Dynamic Type Creation

开发者 https://www.devze.com 2023-01-07 13:44 出处:网络
I\'m trying to create a dynamic type in .Net. I want to get a group of Key Value pairs from a DB table and create a new type of object that has a Property-Value relation.

I'm trying to create a dynamic type in .Net.

I want to get a group of Key Value pairs from a DB table and create a new type of object that has a Property-Value relation.

Example: If I have a row in the table t开发者_运维问答hat has a field that says "Licence Plate" and the other field says "STKOVFL". I want to create an object that has a Property called Licence_Plate, and returns the String "STKOVFL".

Is it possible with introspection?

Best Regards!


Here is an very simple example of what your trying to do.

public class ExampleD : DynamicObject
{
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;

        if (binder.Name == "Licence_Plate")
            result = "STKOVFL";

        return result != null;
    }
}

Console.WriteLine(d.License_Plate);
0

精彩评论

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