强制属性声明在派生类

强制属性声明在派生类

本文介绍了强制属性声明在派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近读到的属性和反思,我认为这将是包括在我的节目元数据的好方法。我有这个抽象类,我想从它继承的类来声明一些属性的所有课程,因为我想我的程序要创建自定义组件(这些派生类),并想读这些课程上运行时的元数据。但是,派生类都必须明确声明我在其中存储元数据的属性。因此,如何迫使我在派生类属性声明?谢谢你。

I recently read about attributes and reflection and I thought it would be a good method to include metadata in my program. I have this abstract class and I wanted all classes inheriting from it to declare with the class some attribute, since I wanted custom components(those derived classes) to be created for my program and wanted to read the metadata of these classes on runtime. However, the derived classes all have to explicitly declare the attribute in which I store metadata. So how to I force an attribute declaration in the derived classes? Thanks.

推荐答案

定义你的属性类本身有那里的继承 AttributeUsageAttribute 属性属性是真正

Define your attribute class to itself have an AttributeUsageAttribute attribute where the Inherited property is true.

或者不这样做,因为这是默认......

Or don't, since that's the default...

衍生的目标(即,如果属性是一类的类,方法,如果它是在一个方法中,等),然后将继承没有明确声明的属性

Derived targets (that is, classes if the attribute is on a class, methods if it is on a method, etc.) will then inherit the attribute without explicit declaration.

这篇关于强制属性声明在派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:24