将匿名类型反序列化为动态类型
我有一个应用程序,其中包含一个称为“任务”的业务实体。该实体具有一组固定的属性,但也具有充当扩展的能力。
因此,它的架构如下所示:
namespace RavenResearch
{
public class Task
{
public Guid TaskId { get; set; }
public DateTime CreatedDate { get; set; }
public dynamic DynamicProperties { get; set; }
}
}
当它存储在 RavenDB 中时,它看起来像这样
{
"TaskId": "9cac592f-98ec-4fda-a823-e5402736078e",
"CreatedDate": "2011-09-22T10:25:35.2701544+12:00",
"DynamicProperties": {
"$type": "<>f__AnonymousType0`2[[System.Int32, mscorlib],[System.String, mscorlib]], RavenResearch",
"MatterNumber": 0,
"CustomerNumber": "37"
}
}
当然,当我尝试从另一个程序查询此数据存储时,它会尝试查找包含 int 和 string 的匿名类型。另一个程序是最初保存文档的 EXE - 我不想引用它。
提取动态属性的最佳方法是什么?我的目标是能够从 Raven 查询 Task 对象的列表,并将它们传递给 Xaml 以在 UI 中渲染 - 这就是为什么 Expando 的数据绑定对我如此有吸引力,在编译时不需要知道属性。
我使用这样的语句创建实例(存储在 Raven 中)
new RavenResearch.Task()
{
TaskId = Guid.NewGuid(),
CreatedDate = DateTime.Now,
DynamicProperties = new
{
MatterNumber = 0,
CustomerNumber = "37"
}
}
@Jacob:如果我使用字典,我将丢失有关动态属性的所有类型信息 - 但是,我可以这样做:
public class Task
{
public Guid TaskId { get; set; }
public DateTime CreatedDate { get; set; }
public Dictionary<string, SimpleValue> DynamicProperties { get; set; }
}
public abstract class SimpleValue
{
}
public class SimpleValue<T> : SimpleValue
{
public T Value
{ get; set; }
public SimpleValue(T value)
{
this.Value = value;
}
}
I have an application that contains a business entity called a 'Task'. This entity has a fixed set of properties, but also the ability to behave as an expando.
So, its schema looks like this:
namespace RavenResearch
{
public class Task
{
public Guid TaskId { get; set; }
public DateTime CreatedDate { get; set; }
public dynamic DynamicProperties { get; set; }
}
}
When this is stored in RavenDB, it look liks this
{
"TaskId": "9cac592f-98ec-4fda-a823-e5402736078e",
"CreatedDate": "2011-09-22T10:25:35.2701544+12:00",
"DynamicProperties": {
"$type": "<>f__AnonymousType0`2[[System.Int32, mscorlib],[System.String, mscorlib]], RavenResearch",
"MatterNumber": 0,
"CustomerNumber": "37"
}
}
Of course, when I attempt to query this datastore from ANOTHER program, it attempts to look for an anonymous type containing an int and string. That other program is the EXE which saved the document originally - I dont want to have to reference this.
What would be the best way to pull out the dynamic properties? My goal is to be able to query a list of Task objects from Raven, and pass them to Xaml for rendering in the UI - which is why databinding to an Expando is so attractive to me, the properties need not be known at compile time.
I create instances (to store in Raven) with statements like this
new RavenResearch.Task()
{
TaskId = Guid.NewGuid(),
CreatedDate = DateTime.Now,
DynamicProperties = new
{
MatterNumber = 0,
CustomerNumber = "37"
}
}
@Jacob: I would lose all type information about the dynamic properties if I used a dictionary - however, I could do something like this:
public class Task
{
public Guid TaskId { get; set; }
public DateTime CreatedDate { get; set; }
public Dictionary<string, SimpleValue> DynamicProperties { get; set; }
}
public abstract class SimpleValue
{
}
public class SimpleValue<T> : SimpleValue
{
public T Value
{ get; set; }
public SimpleValue(T value)
{
this.Value = value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,在运行时,动态只是对象,我们无法知道您的实际含义。
您最好在那里使用 RavenJObject 。这将是处理动态数据的更自然的方式,并且它将保留类型信息
Remember that at runtime, dynamic is simply object, we have no way of knowing what you actually meant there.
You might be better of by using RavenJObject there instead. It would be a more natural way of working with dynamic data, and it will preserve type information
也许在这种情况下避免使用
dynamic
是最好的。如果您使用Dictionary
来代替,创建Task
就不会太可怕了:Maybe avoiding using
dynamic
is best in this case. If you use aDictionary<string, object>
instead, creating aTask
wouldn't be too horrible: