本文介绍了创建属性别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我正在使用的框架和应用程序的代码之间创建一个通用层,但由于框架需要用属性修饰类而被阻止。

I'm trying to create a generic layer between the frameworks I am using and my application's code and have been blocked by a framework's need to decorate my classes with attributes.

是否可以通过某种方式将属性映射到其他属性?

Is there a way to be able to somehow map attributes to other attributes?

示例:
类A用属性B装饰
在运行时,将属性B映射到属性A
在应用程序的整个生命周期中,类A被视为由属性A装饰。

Example:Class A is decorated with Attribute BAt runtime, map Attribute B to Attribute AClass A is seen as decorated by Attribute A throughout the application's life.

推荐答案

您尝试做的事情听起来很糟糕。属性不是运行时类型,实际上它们只是类型元数据。即使某些属性允许您在运行时更改其参数,也不会更改应用的属性。

What you are trying to do sounds like bad design. Attributes are not runtime types, they are in fact just type metadata. Even though some attributes allow you to change their parameters at runtime, that will not change which attribute is applied.

您会在Internet上找到建议使用<$ c的解决方案。 $ c> Reflection.Emit 。请注意,这是一个滑坡,将导致代码难以维护。

You will find solutions on the internet that suggest using Reflection.Emit. Note that this is a slippery slope and will lead to highly unmaintainable code.

我个人的建议是创建自己的 Class B 属性A 属性B 修饰,然后使用内部逻辑进行桥接无论您要桥接的是什么。

My personal suggestion would be to create your own Class B that is decorated with both Attribute A as well as Attribute B, and then use internal logic to bridge whatever it is that you are wanting to bridge.

这篇关于创建属性别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:00