You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi!
I am trying to create DisplayMemberPath functionality with OneWayBind.
I have this function
public static Expression<Func<MenuItemViewModel, string?>> GetLambdaProp(object o, string propName)
{
ParameterExpression param = Expression.Parameter(typeof(MenuItemViewModel));
Expression body = param ;
object obj = o;
Type t = typeof(MenuItemViewModel);
foreach (var member in propName.Split('.'))
{
obj = obj.GetType().GetProperty(member).GetValue(obj) ;
body = Expression.Convert(body, t);
body = Expression.PropertyOrField(body, member);
//remember current parent type
t = obj.GetType();
}
return Expression.Lambda<Func<MenuItemViewModel, string?>>(body, new ParameterExpression[] { param });
}
Using it, gives me the right string:
Expression<Func<MenuItemViewModel, string?>> lambda = GetLambdaProp(ViewModel, "Item." + ViewModel.DisplayMemberPath);
var s = lambda.Compile().Invoke(ViewModel);
but when I use the function within OneWayBind I get the exception
System.ArgumentException: 'Property 'System.String Title' is not defined for type 'System.Object' (Parameter 'property')'
I figured out, I get the same exception when I go with this:
this.OneWayBind(ViewModel, vm=> ((WordTheme)((MenuItemViewModel)vm).Item).Title, v => v.header.Content, ViewModel.Converter).DisposeWith(d);
Where the vm lambda basically is exactly what my GetLambdaProp() function returns. As Item is being cast to WordTheme, the exception that system object doesnt have Title does not make any sense.
(sideinfo: i have to use convert in the function, as the WordTheme gets assigned to the: public Object Item property. Without it, the PropertyOrField triggers a similar exception:
System.ArgumentException: ''Title' is not a member of type 'System.Object' (Parameter 'propertyOrFieldName')'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi!
I am trying to create DisplayMemberPath functionality with OneWayBind.
I have this function
Using it, gives me the right string:
but when I use the function within OneWayBind I get the exception
System.ArgumentException: 'Property 'System.String Title' is not defined for type 'System.Object' (Parameter 'property')'
I figured out, I get the same exception when I go with this:
Where the vm lambda basically is exactly what my GetLambdaProp() function returns. As Item is being cast to WordTheme, the exception that system object doesnt have Title does not make any sense.
(sideinfo: i have to use convert in the function, as the WordTheme gets assigned to the: public Object Item property. Without it, the PropertyOrField triggers a similar exception:
System.ArgumentException: ''Title' is not a member of type 'System.Object' (Parameter 'propertyOrFieldName')'
How can I solve this?
Beta Was this translation helpful? Give feedback.
All reactions