无法转换类型'System.Linq.Expressions.UnaryExpression“对象键入'System.Linq.Expressions.MemberExpression” [英] Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'
问题描述
我创建了C#方法>获取方法名
公共字符串GetCorrectPropertyName< T>(表达式来; Func键< T,串>>表达式)
{
回报率((MemberExpression)expression.Body).Member.Name; //故障点
}
和调用它作为
字符串lcl_name = FALSE;
公共字符串名称
{
{返回lcl_name; }
组
{
lcl_name =价值;
OnPropertyChanged(GetCorrectPropertyName< ThisClassName>(X => x.Name));
}
}
这工作正常,如果属性是字符串,并为所有其他类型给出了这样的例外:
无法投类型的对象System.Linq.Expressions.UnaryExpression为键入'System.Linq.Expressions .MemberExpression。
块引用>
- 我改变字符串对象的方法签名,但随后再次失败。
- 我改变从
X =>呼叫; x.PropertyName
到X => Convert.ToString(x.PropertyName)
,它仍然无法
我在哪里错了?
解决方案您需要一个单独的行以提取成员,其中输入表达式是一元表达。
刚刚转换这个从VB.Net,所以可能会稍微偏离 - 让我知道,如果我需要做任何小的调整:
<预类=郎-CS prettyprint-覆盖>公共字符串GetCorrectPropertyName< T>(表达式来; Func键< T,对象>>表达式)
{
如果(expression.Body是MemberExpression){
回报率((MemberExpression)expression.Body).Member.Name;
}
,否则{
VAR OP =((UnaryExpression)expression.Body).Operand;
回报率((MemberExpression)OP).Member.Name;
}
}
VB的版本是:
<预类=朗动prettyprint-覆盖>公共共享功能GetCorrectPropertyName(Of T)已_
(BYVAL表达作为一种表达(中Func键(中T,对象)))作为字符串
如果TypeOf运算expression.Body是MemberExpression然后
返回DirectCast(expression.Body,MemberExpression).Member.Name
,否则
尺寸OP =(CTYPE( expression.Body,UnaryExpression).Operand)
返回DirectCast(OP,MemberExpression).Member.Name
端如果
端功能
请注意,输入表达式不返回字符串必然 - 这限制你只能读返回字符串的属性。
I created a method in C# to get methodname
public string GetCorrectPropertyName<T>(Expression<Func<T, string>> expression) { return ((MemberExpression)expression.Body).Member.Name; // Failure Point }
and calling it as
string lcl_name = false; public string Name { get { return lcl_name ; } set { lcl_name = value; OnPropertyChanged(GetCorrectPropertyName<ThisClassName>(x => x.Name)); } }
This works fine if property is string and for all other types gives this exception:
Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'.
- I changed string to object in method signature, but then it fails again.
- I changed calling from
x => x.PropertyName
tox => Convert.ToString(x.PropertyName)
and it still failsWhere am I wrong?
解决方案You need a separate line to extract the Member where the input expression is a Unary Expression.
Just converted this from VB.Net, so might be slightly off - let me know if I need to make any minor tweaks:
public string GetCorrectPropertyName<T>(Expression<Func<T, Object>> expression) { if (expression.Body is MemberExpression) { return ((MemberExpression)expression.Body).Member.Name; } else { var op = ((UnaryExpression)expression.Body).Operand; return ((MemberExpression)op).Member.Name; } }
The VB version is:
Public Shared Function GetCorrectPropertyName(Of T) _ (ByVal expression As Expression(Of Func(Of T, Object))) As String If TypeOf expression.Body Is MemberExpression Then Return DirectCast(expression.Body, MemberExpression).Member.Name Else Dim op = (CType(expression.Body, UnaryExpression).Operand) Return DirectCast(op, MemberExpression).Member.Name End If End Function
Note that the input expression does not return string necessarily - that constrains you to only reading properties that return strings.
这篇关于无法转换类型'System.Linq.Expressions.UnaryExpression“对象键入'System.Linq.Expressions.MemberExpression”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!