问题:
我有这个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_
$VB$Local_mytable.tablename
现在我想知道为了在VB.NET中工作我需要改变什么。
有谁知道/有线索?
修改
啊,问题似乎是由项目范围设置中的“Option Infer Off”引起的。
所以这个问题会改变并成为:如何通过“Option Infer Off”来做到这一点?
答案 0 :(得分:2)
你的问题与表达树无关(如果你不愿意包含你得到的错误,这会更容易发现)。
您有Option Infer Off
和Option 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()