【问题标题】:Inconsistent behavior in Expression.PropertyOrFieldExpression.PropertyOrField 中的行为不一致
【发布时间】:2018-01-23 14:22:44
【问题描述】:

第一个表达式通过,但第二个没有。错误是

AmbigiouseMatchExeption

有人知道为什么类的字段和属性行为不一致吗? 如何解决?

【问题讨论】:

  • 考虑编辑你的标签...你的代码是 VB 而不是 C#
  • 更近一步... GetType(Inherited(Of String)).GetField("field1") GetType(Inherited(Of String)).GetProperty("Prop1") 首先是OK 2。不是
  • 是的,不一致是在反射GetField/GetProperty API 中,而不是在Expression 代码中。 “修复”它的唯一方法是不使用它并自行更换。
  • 请将您的代码发布为 text 而不是屏幕截图。目前很难阅读。理想情况下,提供minimal reproducible example

标签: .net vb.net linq linq-expressions


【解决方案1】:

Expression.PropertyOrField 方法使用GetProperty(string,BindingFlags) 来查找属性。这个函数似乎没有实现名称隐藏,而对应的GetField(string,BindingFlags) 实现了。

您需要使用GetProperties 并选择您想要的属性,然后使用带有PropertyInfoExpression.Property 重载。

【讨论】:

    【解决方案2】:

    对于那些感兴趣的人...
    上线失败

    GetType(Inherited(Of String)).GetProperty("Prop1")

    Imports System.Linq.Expressions
    Imports NUnit.Framework
    
    <TestFixture> Public Class ExpressionTEst
    
    <Test> Public Sub GetOvrriddenProperyInExpression()
    
        Dim ex = Expression.PropertyOrField(Expression.Constant(Nothing, GetType(Inherited(Of String))), "field1")
        Dim ex2 = Expression.PropertyOrField(Expression.Constant(Nothing, GetType(Inherited(Of String))), "t2")
    
        GetType(Inherited(Of String)).GetField("field1")
        GetType(Inherited(Of String)).GetProperty("Prop1")
    
        Dim ex3 = Expression.PropertyOrField(Expression.Constant(Nothing, GetType(Inherited(Of String))), "Prop1")
    
    End Sub
    
    Public Class Base
        Public field1 As Object
        Public Property Prop1 As Object
    
        Public Overridable Property t2 As String
    End Class
    
    Public Class Inherited(Of T)
        Inherits Base
    
        Public Overrides Property t2 As String
            Get
                Return MyBase.t2
            End Get
            Set(value As String)
                MyBase.t2 = value
            End Set
        End Property
    
        Public Shadows field1 As T
        Public Shadows Property Prop1 As T
    
    End Class
    

    结束类

    正如@NetMage 和@IvanStoev 所说,不一致之处在于 GetField 与 GetProperty

    这段代码解决了..有点

    Expression memberValue = null;
                if (m.MemberType == MemberTypes.Property)
                {
                    PropertyInfo propInfo = null;
                    foreach(var p in m.DeclaringType.GetProperties())
                    {
                        if (p.Name == m.Name)
                        {
                            //Just pick the first one. 
                            propInfo = p;
                            break;
                        }
                    }
                    memberValue = Expression.Property(Expression.Convert(valueObject, objType), propInfo);
                }
                else
                {
                    memberValue = Expression.Field(Expression.Convert(valueObject, objType), m.Name);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      相关资源
      最近更新 更多