如何在 Cecil 中获取程序集的属性值

发布于 2024-12-20 17:43:22 字数 160 浏览 3 评论 0原文

有没有办法在代码中获取 str1

[MyAttribute("str1")]
class X {}

Mono.Cecil.CustomAttribute.Fields 的实例为空。

Is there a way to get str1 in code ?

[MyAttribute("str1")]
class X {}

The instance of Mono.Cecil.CustomAttribute.Fields is empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-12-27 17:43:22

在 .NET 中使用属性时,您可以使用构造函数参数并设置一些(命名)字段。这在元数据中以不同的方式编码,并在 Cecil 中单独结束。

Mono.Cecil.CustomAttribute.Fields 的实例为空

构造函数参数用于自定义属性时,您使用的是查找字段。所以你要找的是:

type.CustomAttributes[0].ConstructorArguments[0].Value

When using attributes in .NET you are either using the constructor parameters and setting some (named) fields. This is encoded differently in the metadata and ends up separately in Cecil.

the instance of Mono.Cecil.CustomAttribute.Fields is empty

What you're using is looking for fields when the constructor arguments were used for the custom attribute. So what you're looking for is:

type.CustomAttributes[0].ConstructorArguments[0].Value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文