写了如下代码:

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    
public class TypeConditionAttribute<TValue> : Attribute
    {
        
/// <summary>
        
/// The field name of type field
        
/// </summary>
        public string TypeField { getset; }

        
public TValue Value { getset; }

        
public TypeConditionAttribute(string typeField, TValue value)
        {
            TypeField 
= typeField;
            Value 
= value;
        }

        
public bool Evaluate(object record)
        {
            FieldInfo fieldInfo 
= record.GetType().GetField(TypeField);
            
object value = fieldInfo.GetValue(record);
            
return this.Value.Equals((TValue)value);
        }
    }

 

结果不能编译,原因是“Attribute”是一个特性类,无法从它派生泛型类型。有点出乎意外。

 

相关文章:

  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-12-16
  • 2022-12-23
猜你喜欢
  • 2021-12-12
  • 2022-01-19
  • 2021-09-19
  • 2021-07-15
  • 2022-12-23
  • 2021-11-16
  • 2022-01-23
相关资源
相似解决方案