简体   繁体   English

如何创建具有动态属性的类(Expandoobject?)

[英]How to create a class with dynamic property (Expandoobject?)

I have a partial class named Exam. 我有一个名为Exam的局部课程。 The class structure shown as below: 该类的结构如下图所示:

public partial class Exam 
{
    public string ID { set; get; } //default
    public string Name { set; get; } //default
}

After executed the store procedure, the retrieved data should contains one or more test methods as shown as following structure. 执行存储过程后,检索到的数据应包含一种或多种测试方法,如下结构所示。 I would like to put the retrieved data to dynamic object Exam. 我想将检索到的数据放入动态对象考试。 How can I create a dynamic object for object Exam and how can i bind the data to this dynamic object? 如何为对象考试创建动态对象,如何将数据绑定到该动态对象?

ID, Name, Test1, Test2, Test3....
1, Peter, Y, N, Y
1, John, N, Y, Y

public virtual ObjectResult<Exam> sp_exam()
{
    return  ((IObjectContextAdapter)this).
              ObjectContext.ExecuteFunction<Exam>("ConnectionEntities.sp__exam");
}

AFAIK ExpandoObject is just a syntax sugar for a simple dictionary. AFAIK ExpandoObject只是简单字典的语法糖。 So you can write: 所以你可以这样写:

public partial class Exam {
    public string ID { set; get; } //default

    public string Name { set; get; } //default

    public readonly Dictionary<string, object> ExtraProperties = new Dictionary<string, object>();
}

but I guess you just have a bad object model. 但是我猜你只是对象模型不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以创建具有与dynamic或ExpandoObject相同行为的类? - There is any way to create a class with the same behavior as dynamic or ExpandoObject? 如何计算ExpandoObject的属性 - how to count property of an ExpandoObject 如何将动态成员名称和值添加到 ExpandoObject 类的实例? - How to add dynamic member name and value to an instance of ExpandoObject class? 当属性名称是动态的时使用 ExpandoObject,这可能吗? - Using ExpandoObject when property names are dynamic, is this possible? 如何检测 ExpandoObject 上是否存在属性? - How to detect if a property exists on an ExpandoObject? 是否可以使用ExpandoObject创建动态树结构? - Is it possible to create a Dynamic tree structure using the ExpandoObject? 是否可以在动态/ ExpandoObject上创建通用方法 - Is it possible to create a Generic Method on an dynamic / ExpandoObject 获取对内部动态expandoobject属性的引用并为其添加新属性 - Get reference to inner dynamic expandoobject property and add new property for it 为什么“动态”ExpandoObject 即使包含属性定义也会抛出 RuntimeBinderException? - Why 'dynamic' ExpandoObject throws RuntimeBinderException even if it contains the definition for a property? &#39;System.Dynamic.ExpandoObject&#39;不包含名称为&#39;Name&#39;的属性 - 'System.Dynamic.ExpandoObject' does not contain a property with the name 'Name'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM