我可以从在运行时另一个类我的类继承? [英] Can I have my class inherit from another class at run-time?

查看:229
本文介绍了我可以从在运行时另一个类我的类继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类(人),它是我的MVVM应用程序中的一个模型。我想把这个类保存到Azure表存储。要保存到Azure表存储,您需要从一个名为TableServiceEntity的类派生。我宁愿不要混乱我的好的类与属性意味着存储(似乎是一个好主意,考虑分离的问题)。

I have a class (person) that is a model in my MVVM application. I want to save this class to Azure Table Storage. To save to Azure Table Storage, you need to derive from a class called TableServiceEntity. I'd prefer to not clutter my nice class with attributes meant for storage (seems like a good idea in considering separation of concerns).

我可以以某种方式让我的类从运行时派生自TableServiceEntity,在接收对象作为参数的方法中,并将其持久化到存储?这将让我的类保持清洁的存储属性,但仍然让我保存到表存储。

Can I somehow have my class derive from TableServiceEntity at run-time, inside the methods that receive the object as a parameter and persist it to storage? That would let my class stay clean of the storage attributes, but still let me save it to Table Storage.

谢谢!
Andy

Thanks! Andy

推荐答案

您可以使用DynamicObject或 Castle Proxy 。代替将Person保存为azure,您可以使用这些技术之一创建一个AzureEntity,并生成一个具有与Person相同属性的对象,但它继承自TableServiceEntity。

You could simulate something like it using a DynamicObject or Castle Proxy. Instead of saving Person to azure, you could create an AzureEntity using one of those techniques, and generate an object that has all the same properties as Person, yet it inherits from TableServiceEntity.

TableServiceEntity的东西,只是为了方便使用...你可以得到你的手上的存储客户端库源代码(据我记得在git hub或codeplex发布的地方),并修改它工作而不使用TableServiceEntity事件。

The TableServiceEntity thing, is only done for ease of use... You could get your hands on the storage client library source code (which is published somewhere around git hub or codeplex as far as I remember), and modify it to work without using the TableServiceEntity thing.

更新注释:
在您的情况下,您可以使用两者...区别是在expando对象中,您通过扩展定义对象,而动态对象通过理解来定义。动态对象有点更强大(更复杂),但在这种情况下你不需要任何。
BTW:Castle Proxy非常类似于动态对象方法,但它在运行时生成了很多代码,因此它可能达到比动态对象或expando对象更好的性能。

Update for comment: In your case you can use both... The difference is that in expando object you define the object by extension, while the dynamic object is defined by comprehension. Dynamic object is a little more powerful (and more complex), but you don't need any of that in this case. BTW: The Castle Proxy is pretty similar to the dynamic object approach, but it generates a lot of the code at runtime, so it'll probably achieve a far better performance than either the dynamic object, or the expando object.

进一步更新:
您可以使expando对象实现 this

这篇关于我可以从在运行时另一个类我的类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆