gpt4 book ai didi

C# 方法属性

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:50 25 4
gpt4 key购买 nike

有没有不用注入(inject)的方法来实现这个?主题是一个 UserControl。我正在尝试检查标题并将其设置在属性中。

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);
}
}

最佳答案

您正在检查您的类型 的属性。您的属性位于方法 上。更改查询或将属性放在查询预期的类型上:

[Topic(Title = "My Topic")]
public partial class Topic : TopicBase
{
}

关于C# 方法属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com