向系统类添加自定义属性

发布于 2024-12-07 05:37:55 字数 219 浏览 2 评论 0原文

我想从 System.Activites.Presentation 我的自定义属性添加类。我尝试使用 emit(TypeBuilder, ModuleBuilder, AssemblyBuilder) 来实现。是否可以通过向现有类型添加属性来更改现有类型?或者如何告诉TypeBuilder,以便它使用现有的数据类型?或者从给定类型继承? 谢谢。

I want to add classes from System.Activites.Presentation my custom attribute. I tried to do it with emit(TypeBuilder, ModuleBuilder, AssemblyBuilder). Is it possible to change an existing type by adding an attribute to it? Or how to tell TypeBuilder, so that it uses an existing data type? Or inherit from a given type?
Thank you.

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

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

发布评论

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

评论(1

淡紫姑娘! 2024-12-14 05:37:55

您无法向 System 类添加属性,但如果它们未标记为 Sealed,您可以创建从原始派生的自定义类并添加自定义属性。

您的所有代码都必须调用派生类,除了添加的属性之外,派生类与原始类相同。

[MyAttribute(DisplayName="Name shown")]
public class MyActivity: System.Activities.Activity
{
}
/// <summary>
/// Custom attribute definition
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class MyAttribute : System.Attribute
{


    /// <summary>
    /// Defines the attribute
    /// </summary>
         public string DisplayName { get; set; }
    /// <summary>
    /// Allow access to the attribute
    /// </summary>
    /// <param name="prop"></param>
    /// <returns></returns>
      public static string GetDisplayName(System.Reflection.MemberInfo prop)
    {
        string field = null;
        object[] attr = prop.GetCustomAttributes(false);
        foreach (object a in attr)
        {
            MyAttribute additional = a as MyAttribute;
            if (additional != null)
            {
                field = additional.DisplayName;
            }
        }
        return field;
    }


}

You can't add attributes to System classes but, if they are not marked as Sealed you can create your custom class deriving from original and adding custom attribute.

All your code must call derived class, that is identical to the original except on the attribute added.

[MyAttribute(DisplayName="Name shown")]
public class MyActivity: System.Activities.Activity
{
}
/// <summary>
/// Custom attribute definition
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class MyAttribute : System.Attribute
{


    /// <summary>
    /// Defines the attribute
    /// </summary>
         public string DisplayName { get; set; }
    /// <summary>
    /// Allow access to the attribute
    /// </summary>
    /// <param name="prop"></param>
    /// <returns></returns>
      public static string GetDisplayName(System.Reflection.MemberInfo prop)
    {
        string field = null;
        object[] attr = prop.GetCustomAttributes(false);
        foreach (object a in attr)
        {
            MyAttribute additional = a as MyAttribute;
            if (additional != null)
            {
                field = additional.DisplayName;
            }
        }
        return field;
    }


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