vb.net vs C# lambda expression


November 2018


299 time


I am facing a strange problem when parsing a very simple lambda expression.

In C#, i have this

class Class1

class Class2 : Class1
{ }

class Invoker
    public System.Linq.Expressions.Expression<Func<Class1>> GetExpression()
        return () => new Class2();

This, as expected returns an expression with NodeType = New

In VB.NET, i have

Public Class Class1

End Class

Public Class Class2
    Inherits Class1

End Class

Public Class Invoker
    Public Function GetExpression() As Expression(Of Func(Of Class1))
        Return Function() New Class2()
    End Function

End Class

This returns a lambda expression with NodeType = Convert (?????)

I suppose it has to do with conversion from Expression(Of Func(Of Class2)) to Expression(Of Func(Of Class1))

I tried playing with project properties like Option Strict... I can't get it to work in VB.

Is there a way i can get the correct lambda expression in VB? I need this to implement an EnterpriseLibrary TraceListener. The base class calling the GetExpression() validates the lambda and tries to cast it to System.Linq.Expressions.NewExpression (which fails in VB since it is a UnaryExpression because of the Convert).



1 answers


Когда я запускаю обе части кода в Visual Studio (V2008, Console Application 64bit, 3,5 рамки), они оба возвращают выражение с NodeType == ExpressionType.Lambda