向类的每个属性添加其他属性 [英] Adding additional attributes to each property of a class

查看:88
本文介绍了向类的每个属性添加其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个带有任何类型的任意数量属性的类

Say I have a class with any number of properties of any type

public class Test
{
public string A {get;set;}
public object B {get;set;}
public int C {get;set;}
public CustomClass D {get;set;}
}

我希望此类中的所有对象都具有错误"和警告"的概念.例如,基于另一个类中的某些条件,我可能想在属性A上设置警告,然后在GUI中显示该信息. GUI与我无关.我应该如何设置此警告?我希望能够做类似的事情:

I want all of the objects inside this class to have a notion of 'errors' and 'warnings'. For example, based on some condition in another class, I might want to set a warning on property A, and then display that information in the GUI. The GUI is not my concern; rather how should I set this warning? I would love to be able to do something like:

为类中的每个属性添加一个警告"和错误"属性,以便我可以这样做.

For every property in a class, add a 'Warning' and 'Error' property such that I can do..

Test t = new Test();
t.A.Warning = "This is a warning that the GUI will know to display in yellow".
t.B.Error = null;

执行此操作的最佳方法是什么?如果可以在类的每个属性中添加一个自定义属性,以添加这些附加属性,并允许我以一种清晰的方式访问它们,那将是很好的.

What is the best way to go about doing this? It would be nice if I could add a custom attribute to each property of the class that would add these additional properties and allow me to access them in a clear way.

我看到了将字典添加到父类(测试),然后传递与属性名称匹配的字符串,或使用反射来获取属性名称并将其传递的解决方案,但是我希望使用更简洁的方法.

I have seen solutions that add a Dictionary to the parent class (Test), and then pass in strings that match the property names, or use reflection to get the property name and pass that in, but I would prefer something cleaner.

推荐答案

您可以在属性中添加所需的自定义属性,然后在对象上使用扩展方法来访问这些属性.

You could add a custom attribute that you want to the property and then use an extension method on object to access those attributes.

类似的事情应该起作用

首先,您需要创建属性类

First you will need to create your attribute class

[AttributeUsage(AttributeTargets.All/*, AllowMultiple = true*/)]
public class WarningAttribute : System.attribute
{
   public readonly string Warning;

   public WarningAttribute(string warning)
   {
      this.Warning = warning;
   }    
}

更多阅读此处

按原样使用

[WarningAttribute("Warning String")]
public string A {get;set;}

然后按 MSDN文章

public static string Warning(this Object object) 
{
    System.Attribute[] attrs = System.Attribute.GetCustomAttributes(object);

    foreach (System.Attribute attr in attrs)
        {
            if (attr is WarningAttrbiute)
            {
                return (WarningAttribute)attr.Warning;
            }
        } 
}

然后,如果您有想要访问警告的项目,只需致电

Then if you have an item that you want to access the warning on you can simply call

test.A.Warning;

如果您想设置警告字符串,则可以更干净地实现某种设置方法.可能通过设置辅助对象或属性类型来实现.

If you're wanting to set the warning string you could implement some kind of setter for that a little more cleanly. Potentially through setting a secondary object or property type.

执行此操作的另一种方法是,不仅可以使用stringobject,还可以创建一个自定义的泛型类型来处理该属性设置.

An alternative method to do this would be instead of just using string and object you could create a custom generic type to handle that property setting.

类似

public class ValidationType<T>
{
   public T Value {get; set;}
   public string Warning {get; set;}
   public string Error {get; set;}

   public ValidationType(T value)
   {
      Value = value;
   }
}

使用方式

var newWarning = new ValidationType<string>("Test Type");
newWarning.Warning = "Test STring";
Console.WriteLine("newWarning.Value");

这篇关于向类的每个属性添加其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆