创建属性别名?

编程入门 行业动态 更新时间:2024-10-22 07:26:24
本文介绍了创建属性别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 B At runtime, map Attribute B to Attribute A Class 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上找到建议使用 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.

更多推荐

创建属性别名?

本文发布于:2023-10-26 18:56:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!