属性需要另一个属性。 [英] Attribute requiring another attribute.

查看:82
本文介绍了属性需要另一个属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有"开箱即用"的内容。是否允许我以一种需要存在其他属性的方式定义自定义属性?

Is there anything "out of the box" that lets me define a custom attribute in such a way that it requires the presence of some other attribute?

我想阻止人们在属性上使用某些属性,除非他们还附加了一些其他属性也是。

I want to prevent people using some attribute on a property unless they also attach some other attribute too.

Thx

推荐答案

编译器不会执行任何操作。

There is nothing that the compiler will enforce.

你当然,可以在运行时使用反射来调查属性所附加的项目,如果缺少则抛出异常。

You can, of course, use reflection at runtime to investigate what items your attribute is attached to and throw an exception if it is missing.

我曾遇到过相反的问题(两个属性在逻辑上是相互的这是我最终解决问题的方法。

I once had the opposite problem (two attributes were logically mutually exclusive) and that is how I eventually resolved the issue.


这篇关于属性需要另一个属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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