.net 属性构造函数中的Lambda表达式

uyto3xhc  于 2023-03-13  发布在  .NET
关注(0)|答案(7)|浏览(105)

我创建了一个名为RelatedPropertyAttributeAttribute类:

[AttributeUsage(AttributeTargets.Property)]
public class RelatedPropertyAttribute: Attribute
{
    public string RelatedProperty { get; private set; }

    public RelatedPropertyAttribute(string relatedProperty)
    {
        RelatedProperty = relatedProperty;
    }
}

我用它来表示类中的相关属性。我将如何使用它的示例:

public class MyClass
{
    public int EmployeeID { get; set; }

    [RelatedProperty("EmployeeID")]
    public int EmployeeNumber { get; set; }
}

我想使用lambda表达式,这样我就可以把一个强类型传递给我的属性的构造函数,而不是一个“魔术字符串”。这样我就可以利用编译器的类型检查。例如:

public class MyClass
{
    public int EmployeeID { get; set; }

    [RelatedProperty(x => x.EmployeeID)]
    public int EmployeeNumber { get; set; }
}

我想我可以用下面的代码来实现,但是编译器不允许这样做:

public RelatedPropertyAttribute<TProperty>(Expression<Func<MyClass, TProperty>> propertyExpression)
{ ... }

错误:
非泛型类型“RelatedPropertyAttribute”不能与类型参数一起使用
我怎样才能做到这一点?

1l5u6lss

1l5u6lss1#

拥有泛型属性是不可能的。然而C#和VB不支持它,但CLR支持。如果你想写一些IL代码,这是可能的。
让我们看看你的代码:

[AttributeUsage(AttributeTargets.Property)]
public class RelatedPropertyAttribute: Attribute
{
    public string RelatedProperty { get; private set; }

    public RelatedPropertyAttribute(string relatedProperty)
    {
       RelatedProperty = relatedProperty;
    }
}

编译代码,用ILSpyILDasm打开程序集,然后将内容转储到一个文本文件。属性类声明的IL将如下所示:

.class public auto ansi beforefieldinit RelatedPropertyAttribute
extends [mscorlib]System.Attribute

在文本文件中,您可以将属性设置为泛型。有几个方面需要更改。
这可以简单地通过更改IL来完成,CLR不会抱怨:

.class public abstract auto ansi beforefieldinit
      RelatedPropertyAttribute`1<class T>
      extends [mscorlib]System.Attribute

现在可以将 relatedProperty 的类型从string更改为泛型类型。
例如:

.method public hidebysig specialname rtspecialname 
    instance void .ctor (
        string relatedProperty
    ) cil managed

将其更改为:

.method public hidebysig specialname rtspecialname 
    instance void .ctor (
        !T relatedProperty
    ) cil managed

有很多框架可以做这样的“肮脏”工作:Mono.CecilCCI中的任意一个。
正如我已经说过的,它不是一个干净的面向对象的解决方案,但只是想指出另一种方法来打破C#和VB的限制。
围绕这个主题有一本有趣的阅读check it out this book。
希望有帮助。

jtw3ybtb

jtw3ybtb2#

你不能

  • 不再正确:你不能创建泛型属性,耶!你不能创建泛型属性类型(这是不允许的);同样,没有定义 using 通用属性([Foo<SomeType>])的语法
  • 你不能在属性初始化器中使用lambda--传递给属性的值非常有限,并且不包括表达式(表达式非常复杂,并且是运行时对象,不是编译时文字)
fxnxkyjh

fxnxkyjh3#

如果使用的是C# 6.0,则可以使用nameof
用于获取简单变量、类型或成员的(非限定的)字符串名称。当报告代码中的错误时,挂接模型视图控制器(MVC)链接、触发属性更改事件等,您通常希望捕获方法的字符串名称。使用nameof有助于在重命名定义时保持代码有效。在必须使用字符串常量引用定义之前,这在重命名代码元素时是脆弱的,因为工具不知道检查这些字符串文字。
你可以像这样使用你的属性:

public class MyClass
{
    public int EmployeeID { get; set; }

    [RelatedProperty(nameof(EmployeeID))]
    public int EmployeeNumber { get; set; }
}
t3psigkw

t3psigkw4#

可能的解决方法之一是为每个属性关系定义类,并通过
属性构造函数中的typeof()运算符。

更新日期:

例如:

[AttributeUsage(AttributeTargets.Property)]
public class RelatedPropertyAttribute : Attribute
{
    public Type RelatedProperty { get; private set; }

    public RelatedPropertyAttribute(Type relatedProperty)
    {
        RelatedProperty = relatedProperty;
    }
}

public class PropertyRelation<TOwner, TProperty>
{
    private readonly Func<TOwner, TProperty> _propGetter;

    public PropertyRelation(Func<TOwner, TProperty> propGetter)
    {
        _propGetter = propGetter;
    }

    public TProperty GetProperty(TOwner owner)
    {
        return _propGetter(owner);
    }
}

public class MyClass
{
    public int EmployeeId { get; set; }

    [RelatedProperty(typeof(EmployeeIdRelation))]
    public int EmployeeNumber { get; set; }

    public class EmployeeIdRelation : PropertyRelation<MyClass, int>
    {
        public EmployeeIdRelation()
            : base(@class => @class.EmployeeId)
        {

        }
    }
}
7hiiyaii

7hiiyaii5#

你不能。属性类型被限制为here。我的建议是,尝试在外部计算你的lambda表达式,然后使用以下类型之一:

  • 简单类型(bool、byte、char、short、int、long、float和double)
  • System.Type
  • 枚举
  • object(object类型的属性参数的实参必须是上述类型之一的常数值。)
  • 上述任意类型的一维数组
hm2xizp9

hm2xizp96#

为了扩展我的评论,这是一种用不同的方法来完成任务的方法。您说您想要“指示类中的相关属性”,并且您“想要使用lambda表达式以便我可以将强类型传递到属性的构造函数中,而不是“魔术字符串”。这样我就可以利用编译器类型检查"。
下面是一种指示编译时类型化且没有任何幻字符串的相关属性的方法:

public class MyClass
{
    public int EmployeeId { get; set; }
    public int EmployeeNumber { get; set; }
}

这是我们要考虑的类。我们希望指出EmployeeIdEmployeeNumber是相关的。为了代码简洁,让我们把这个类型别名放在代码文件的顶部。这根本不是必须的,但它确实使代码不那么令人生畏:

using MyClassPropertyTuple = 
    System.Tuple<
            System.Linq.Expressions.Expression<System.Func<MyClass, object>>,
            System.Linq.Expressions.Expression<System.Func<MyClass, object>>
        >;

这使得MyClassPropertyTuple成为两个ExpressionTuple的别名,每个Expression捕获从MyClass到对象的函数的定义。例如,MyClass上的属性getter就是这样的函数。
现在让我们来捕捉这个关系,这里我在MyClass上做了一个静态属性,但是这个列表可以在任何地方定义:

public class MyClass
{
    public static List<MyClassPropertyTuple> Relationships
        = new List<MyClassPropertyTuple>
            {
                new MyClassPropertyTuple(c => c.EmployeeId, c => c.EmployeeNumber)
            };
}

C#编译器知道我们正在构造Expression s的Tuple,所以我们不需要在那些lambda表达式前面进行任何显式强制转换--它们会自动转换为Expression s。
基本上就定义而言就是这样了--**那些EmployeeIdEmployeeNumber都是强类型的,并且在编译时被强制执行,而执行属性重命名的重构工具应该能够在重命名过程中找到这些用法(ReSharper当然可以)。
当然,我们也希望能够在运行时查询关系(我假设!)。我不知道你想如何做到这一点,所以这段代码只是说明。

class Program
{
    static void Main(string[] args)
    {
        var propertyInfo1FromReflection = typeof(MyClass).GetProperty("EmployeeId");
        var propertyInfo2FromReflection = typeof(MyClass).GetProperty("EmployeeNumber");

        var e1 = MyClass.Relationships[0].Item1;

        foreach (var relationship in MyClass.Relationships)
        {
            var body1 = (UnaryExpression)relationship.Item1.Body;
            var operand1 = (MemberExpression)body1.Operand;
            var propertyInfo1FromExpression = operand1.Member;

            var body2 = (UnaryExpression)relationship.Item2.Body;
            var operand2 = (MemberExpression)body2.Operand;
            var propertyInfo2FromExpression = operand2.Member;

            Console.WriteLine(propertyInfo1FromExpression.Name);
            Console.WriteLine(propertyInfo2FromExpression.Name);

            Console.WriteLine(propertyInfo1FromExpression == propertyInfo1FromReflection);
            Console.WriteLine(propertyInfo2FromExpression == propertyInfo2FromReflection);
        }
    }
}

这里的propertyInfo1FromExpressionpropertyInfo2FromExpression代码是我在调试时明智地使用了Watch窗口编写的--通常我就是这样计算Expression树实际包含的内容的。
运行此程序将生成

EmployeeId
EmployeeNumber
True
True

这表明我们可以成功地提取相关属性的细节,而且(至关重要的是)* 它们与通过其他方式获得的PropertyInfo具有相同的引用 *。希望您可以将此方法与实际使用的任何方法结合使用,以在运行时指定感兴趣的属性。

6ie5vjzr

6ie5vjzr7#

提示:使用nameof。我有一个DateRangeAttribute,它验证两个属性并确保它们是有效的DateRange。

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
 public class DateRangeAttribute : ValidationAttribute
 {
      private readonly string _endDateProperty;
      private readonly string _startDateProperty;

      public DateRangeAttribute(string startDateProperty, string endDateProperty) : base()
      {
            _startDateProperty = startDateProperty;
            _endDateProperty = endDateProperty;
      }

      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
            var stP = validationContext.ObjectType.GetProperty(_startDateProperty);
            var enP = validationContext.ObjectType.GetProperty(_endDateProperty);
            if (stP == null || enP == null || stP.GetType() != typeof(DateTime) || enP.GetType() != typeof(DateTime))
            {
                 return new ValidationResult($"startDateProperty and endDateProperty must be valid DateTime properties of {nameof(value)}.");
            }
            DateTime start = (DateTime)stP.GetValue(validationContext.ObjectInstance, null);
            DateTime end = (DateTime)enP.GetValue(validationContext.ObjectInstance, null);

            if (start <= end)
            {
                 return ValidationResult.Success;
            }
            else
            {
                 return new ValidationResult($"{_endDateProperty} must be equal to or after {_startDateProperty}.");
            }
      }
 }

class Tester
{
    public DateTime ReportEndDate { get; set; }
    [DateRange(nameof(ReportStartDate), nameof(ReportEndDate))]
    public DateTime ReportStartDate { get; set; }
}

相关问题