Can I have my class inherit from another class at run-time?

人盡茶涼 提交于 2019-12-10 21:08:20

问题


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

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.

Thanks! Andy


回答1:


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.

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.

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.

Further update: You can make expando object implement an interface like this.




回答2:


You cannot change inheritance at runtime.

What you can do is map from your domain object to a data storage object derived from TableServiceEntity using a tool such as AutoMapper.

One of the benefits of O/R Mappers like Entity Framework Code First or NHibernate is that they do not force you to derive from a special base class.




回答3:


Unfortunately, no: inheritance in C# is a compile-time concept, so your object must inherit from its base class at compile time.

As far as the "cluttering your code with attributes meant for storage" goes, partial classes could very much help: put storage-related attributes in a separate file, and your code will look clean again.




回答4:


It sounds like it would be simpler to just create a PersonTableServiceEntity class and a static method to translate between your Person entity and the PersonTableServiceEntity




回答5:


Short answer-- NO. It's all done at compile time in c#. Srry




回答6:


No idea at all if that's possible, but it doesn't strike me as likely. Just create a PersonStorage class to inherit TableServiceEntity with a single Person instance?



来源:https://stackoverflow.com/questions/11572531/can-i-have-my-class-inherit-from-another-class-at-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!