gpt4 book ai didi

c# - 如何在 C# 中访问类的属性

转载 作者:太空狗 更新时间:2023-10-30 00:53:46 25 4
gpt4 key购买 nike

我有这个类的这些属性,我想知道如何从类中访问它们。 ServedClassName 是一个自定义属性,这是我实际尝试访问的属性。

[Guid("24889af6-e174-460b-ab52-7fb5a925926e")]
[ServedClassName("ASCOM ProxyClient Telescope")]
[ProgId("ASCOM.DeviceProxyClient.Telescope")]
[ClassInterface(ClassInterfaceType.None)]
public class Telescope : ReferenceCountedObjectBase, ITelescopeV3

为了访问 ProgID,我使用了这个:Marshal.GenerateProgIdForType(this.GetType());

最佳答案

object [] attrs = GetType().GetCustomAttributes(typeof(ServedClassNameAttribute), true);

将为您提供类中 ServedClassNameAttribute 类型的自定义属性列表。然后,您可以像这样遍历属性实例:

foreach (ServedClassNameAttribute attr in attrs)
{
// Do something with attr
}

关于c# - 如何在 C# 中访问类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15141658/

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