<i id='6bYp6'><tr id='6bYp6'><dt id='6bYp6'><q id='6bYp6'><span id='6bYp6'><b id='6bYp6'><form id='6bYp6'><ins id='6bYp6'></ins><ul id='6bYp6'></ul><sub id='6bYp6'></sub></form><legend id='6bYp6'></legend><bdo id='6bYp6'><pre id='6bYp6'><center id='6bYp6'></center></pre></bdo></b><th id='6bYp6'></th></span></q></dt></tr></i><div id='6bYp6'><tfoot id='6bYp6'></tfoot><dl id='6bYp6'><fieldset id='6bYp6'></fieldset></dl></div>

  • <small id='6bYp6'></small><noframes id='6bYp6'>

      • <bdo id='6bYp6'></bdo><ul id='6bYp6'></ul>
    1. <tfoot id='6bYp6'></tfoot>

      1. <legend id='6bYp6'><style id='6bYp6'><dir id='6bYp6'><q id='6bYp6'></q></dir></style></legend>

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

        时间:2023-07-27
        <legend id='QX1fn'><style id='QX1fn'><dir id='QX1fn'><q id='QX1fn'></q></dir></style></legend>
            <tbody id='QX1fn'></tbody>

            <i id='QX1fn'><tr id='QX1fn'><dt id='QX1fn'><q id='QX1fn'><span id='QX1fn'><b id='QX1fn'><form id='QX1fn'><ins id='QX1fn'></ins><ul id='QX1fn'></ul><sub id='QX1fn'></sub></form><legend id='QX1fn'></legend><bdo id='QX1fn'><pre id='QX1fn'><center id='QX1fn'></center></pre></bdo></b><th id='QX1fn'></th></span></q></dt></tr></i><div id='QX1fn'><tfoot id='QX1fn'></tfoot><dl id='QX1fn'><fieldset id='QX1fn'></fieldset></dl></div>

              <small id='QX1fn'></small><noframes id='QX1fn'>

              <tfoot id='QX1fn'></tfoot>

                • <bdo id='QX1fn'></bdo><ul id='QX1fn'></ul>
                  本文介绍了属性构造函数中的 Lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我创建了一个名为 RelatedPropertyAttributeAttribute 类:

                  I have created an Attribute class called RelatedPropertyAttribute:

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

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

                  I use this to indicate related properties in a class. Example of how I would use it:

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

                  我想使用 lambda 表达式,以便将强类型传递给属性的构造函数,而不是魔术字符串".这样我可以利用编译器类型检查.例如:

                  I would like to use lambda expressions so that I can pass a strong type into my attribute's constructor, and not a "magic string". This way I can exploit compiler type checking. For example:

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

                  我以为我可以用以下方法做到这一点,但编译器不允许这样做:

                  I thought I could do it with the following, but it isn't allowed by the compiler:

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

                  错误:

                  非泛型类型RelatedPropertyAttribute"不能与类型参数

                  The non-generic type 'RelatedPropertyAttribute' cannot be used with type arguments

                  我怎样才能做到这一点?

                  How can I achieve this?

                  推荐答案

                  你不能

                  • 您不能创建通用属性类型(根本不允许这样做);同样,没有定义 使用 通用属性 ([Foo<SomeType>]) 的语法
                  • 您不能在属性初始值设定项中使用 lambda - 可传递给属性的值非常有限,并且根本不包括表达式(非常复杂,并且是运行时对象,而不是编译时文字)
                  • you cannot create generic attribute types (it simply isn't allowed); equally, no syntax for using generic attributes ([Foo<SomeType>]) is defined
                  • you cannot use lambdas in attribute initializers - the values available to pass to attributes is very limited, and simply does not include expressions (which are very complex, and are runtime objects, not compile-time literals)

                  这篇关于属性构造函数中的 Lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在与 KnownType 属性一起使用的类上使用 Obsolete 属性时如何忽略编译器警告 下一篇:C# 是否有“ThreadLocal"?模拟(对于数据成员)到“ThreadStatic";属性?

                  相关文章

                  <legend id='bikMO'><style id='bikMO'><dir id='bikMO'><q id='bikMO'></q></dir></style></legend>
                • <i id='bikMO'><tr id='bikMO'><dt id='bikMO'><q id='bikMO'><span id='bikMO'><b id='bikMO'><form id='bikMO'><ins id='bikMO'></ins><ul id='bikMO'></ul><sub id='bikMO'></sub></form><legend id='bikMO'></legend><bdo id='bikMO'><pre id='bikMO'><center id='bikMO'></center></pre></bdo></b><th id='bikMO'></th></span></q></dt></tr></i><div id='bikMO'><tfoot id='bikMO'></tfoot><dl id='bikMO'><fieldset id='bikMO'></fieldset></dl></div>

                    <tfoot id='bikMO'></tfoot>

                    <small id='bikMO'></small><noframes id='bikMO'>

                        <bdo id='bikMO'></bdo><ul id='bikMO'></ul>