Linq.Expression GetValue in VB?

本文关键字:VB in GetValue Expression Linq | 更新日期: 2023-09-27 18:17:06

问题:

我有一个C#程序,它获取mytable的字段tablename的值
而且效果很好。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace AttachObjectsCS
{

    static class Program
    {

        public class cmytable
        {
            public string tablename = "test";
        }

        // http://stackoverflow.com/questions/2616638/access-the-value-of-a-member-expression
        private static object GetValue(System.Linq.Expressions.MemberExpression member)
        {
            System.Linq.Expressions.Expression objectMember = System.Linq.Expressions.Expression.Convert(member, typeof(object));
            System.Linq.Expressions.Expression<Func<object>> getterLambda = System.Linq.Expressions.Expression.Lambda<Func<object>>(objectMember);
            var getter = getterLambda.Compile();
            return getter();
        }

        public static void AddField<T>(System.Linq.Expressions.Expression<Func<T>> expr, string alias)
        {
            var body = ((System.Linq.Expressions.MemberExpression)expr.Body);
            Console.WriteLine("Name is: {0}", body.Member.Name);

            object obj = GetValue(body);

            Console.WriteLine("Value is: {0}", obj);
        }

        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (false)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

            cmytable mytable = new cmytable();
            AddField(() => mytable.tablename, "te");

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(" --- Press any key to continue --- ");
            Console.ReadKey();
        } // End Sub Main 

    } // End Class Program

} // End Namespace AttachObjectsCS

现在我需要VB.NET中的相同功能,但它失败了
现在,由于我知道VB.NET和C#并不总是使用相同的linq表达式,所以我在这里遇到问题并不感到惊讶。

Module Program


    Public Class cmytable
        Public tablename As String = "test"
    End Class

    Public Sub AddField(Of T)(expr As System.Linq.Expressions.Expression(Of Func(Of T)))
        Dim body = DirectCast(expr.Body, System.Linq.Expressions.MemberExpression)
        Dim strName As String = body.Member.Name
        Console.WriteLine("Name is: {0}", strName)

        Dim obj As Object = GetValue(body)

        Console.WriteLine("Value is: {0}", obj)
    End Sub

    ' http://stackoverflow.com/questions/2616638/access-the-value-of-a-member-expression '
    Private Function GetValue(member As System.Linq.Expressions.MemberExpression) As Object
        Dim objectMember As System.Linq.Expressions.Expression = System.Linq.Expressions.Expression.Convert(member, GetType(Object))
        Dim getterLambda As System.Linq.Expressions.Expression(Of Func(Of Object)) = System.Linq.Expressions.Expression.Lambda(Of Func(Of Object))(objectMember)

        Dim getter = getterLambda.Compile()
        Return getter()
    End Function

    Public Sub Main()
        Dim mytable As New cmytable
        AddField(Function() mytable.tablename)
    End Sub

End Module ' Program

问题出在GetValue中,问题是我似乎没有合适的objectMember。

这里是表达式的调试输出:

CS objectMember = {Convert(value(AttachObjectsCS.Program+<>c__DisplayClass0).mytable.tablename)}
VB objectMember = {Convert(value(AttachObjects.Program+_Closure$__1).$VB$Local_mytable.tablename)}
CS getterLambda = {() => Convert(value(AttachObjectsCS.Program+<>c__DisplayClass0).mytable.tablename)}
VB getterLambda = {() => Convert(value(AttachObjects.Program+_Closure$__1).$VB$Local_mytable.tablename)}

我的猜测是问题出在$VB$Local_mytable.tablename 中的$VB$Local_

现在我想知道为了在VB.NET中工作,我必须更改什么。
有人知道/有线索吗?

编辑:
啊,这个问题似乎是由项目范围设置中的"选项推断关闭"引起的
因此,问题发生了变化,变成了:如何通过"选项推断"来做到这一点?

Linq.Expression GetValue in VB?

您的问题与表达式树无关(如果您费心将得到的错误包括在内,则会更容易发现(。

您有Option Infer OffOption Strict Off以及以下代码:

Dim getter = getterLambda.Compile()
Return getter()

抛出MissingMemberException:

找不到类型"Func(Of Object("的默认成员。

问题是()在这里被解释为调用默认成员而不是调用委托,因为getter的类型是Object。要解决此问题,您可以指定变量的类型:

Dim getter As Func(Of Object) = getterLambda.Compile()
Return getter()

或者,您可以使用更详细的语法来调用委托:

Dim getter = getterLambda.Compile()
Return getter.Invoke()