http://nhcontrib.svn.sourceforge.net/viewvc/nhcontrib/tags/NHibernate.Validator/1.2.0GA/src/NHibernate.Validator/Util/TypeUtils.cs?revision=1328&view=markup

Line 135

Change: expression.Body.Type == typeof (object)
To: expression.Body.Type == typeof (TResult)

Resolves a bug with VB.NET and collection properties where the
Body.Type is "IEnumerable<TEntity>" and not "object".

LINQPad test case:

Sub Main()
        DecodeMemberAccessExpression(Of Thing, IEnumerable(Of Thing))
(Function(t) t.Things).Name.Dump()
End Sub

' Define other methods and classes here
Public Shared Function DecodeMemberAccessExpression(Of TEntity,
TResult)(expression As Expression(Of Func(Of TEntity, TResult))) As
MemberInfo
        expression.Dump()
        If expression.Body.NodeType <> ExpressionType.MemberAccess Then
                If (expression.Body.NodeType = ExpressionType.Convert) AndAlso
(expression.Body.Type = GetType(object)) Then
                        Return DirectCast(DirectCast(expression.Body,
UnaryExpression).Operand, MemberExpression).Member
                End If
                Throw New Exception(String.Format("Invalid expression type: 
Expected
ExpressionType.MemberAccess, Found {0}", expression.Body.NodeType))
        End If
        Return DirectCast(expression.Body, MemberExpression).Member
End Function

Public Class Thing

        Public Property Name() As String
                Get
                        Return m_Name
                End Get
                Set
                        m_Name = Value
                End Set
        End Property
        Private m_Name As String

        Public Property Things() As IList(Of Thing)
                Get
                        Return m_Things
                End Get
                Set
                        m_Things = Value
                End Set
        End Property
        Private m_Things As IList(Of Thing)
End Class

Working function:

Public Shared Function DecodeMemberAccessExpression(Of TEntity,
TResult)(expression As Expression(Of Func(Of TEntity, TResult))) As
MemberInfo
        expression.Dump()
        If expression.Body.NodeType <> ExpressionType.MemberAccess Then
                If (expression.Body.NodeType = ExpressionType.Convert) AndAlso
(expression.Body.Type = GetType(TResult)) Then
                        Return DirectCast(DirectCast(expression.Body,
UnaryExpression).Operand, MemberExpression).Member
                End If
                Throw New Exception(String.Format("Invalid expression type: 
Expected
ExpressionType.MemberAccess, Found {0}", expression.Body.NodeType))
        End If
        Return DirectCast(expression.Body, MemberExpression).Member
End Function

-- 
You received this message because you are subscribed to the Google Groups 
"NHibernate Contrib - Development Group" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhcdevs?hl=en.

Reply via email to