【发布时间】:2015-08-25 06:39:13
【问题描述】:
有没有办法在不注入的情况下实现这一点?主题是一个用户控件。我正在尝试检查标题并将其设置在属性中。
public partial class Topic: TopicBase
{
[Topic(Title = "My Topic")]
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
}
当我在 TopicBase.cs 中查找属性时,我得到 null
protected override void OnInit(EventArgs e)
{
var topicAttr = Attribute.GetCustomAttribute(this.GetType(), typeof(TopicAttribute)); //topicAttr is null here.
if (topicAttr != null)
{
SetTopic(((TopicAttribute)topicAttr).Title);
}
}
【问题讨论】:
标签: c# reflection custom-attributes