-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix DynamicIndex implementation #480
Conversation
Hello @JonathanMagnan, Can you or a developer please take a look at this PR ? This PR solves issue #448 but in order to fix that I had to remove the DynamicIndex logic which was introduced in 1.1.8 and use the original implementation (with a small change) to make sure that #397 keeps working correctly. For that, I did enable the unittest again: Please review the changes. |
@StefH with following implementation I was able to get a robust solution for my problem as well as un-comment internal class DynamicGetMemberBinder : GetMemberBinder
{
private static readonly Type DynamicWrapperType = typeof(DynamicWrapper);
private static readonly ConstructorInfo DynamicWrapperConstructor = DynamicWrapperType.GetConstructor(new[] {typeof(object)});
private static readonly PropertyInfo DynamicWrapperTypeIndexer = DynamicWrapperType.GetProperty("Item");
public DynamicGetMemberBinder(string name, [CanBeNull] ParsingConfig config) : base(name, !(config?.IsCaseSensitive == true))
{
}
public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
{
var instance = Expression.New(DynamicWrapperConstructor, target.Expression);
var indexExpression = Expression.MakeIndex(instance, DynamicWrapperTypeIndexer, new Expression[] {Expression.Constant(Name)});
return DynamicMetaObject.Create(target.Value, indexExpression);
}
private class DynamicWrapper
{
private readonly object _item;
private readonly Dictionary<string, PropertyInfo> _properties;
public DynamicWrapper(object item)
{
_item = item;
if (item != null && !(item is IDictionary<string, object>))
{
_properties = item.GetType().GetProperties()
.ToDictionary(p => p.Name, d => d);
}
}
public object this[string value]
{
get
{
if (_item is null)
throw new NullReferenceException();
if (_item is IDictionary<string, object> dictionary)
return dictionary[value];
if (_properties.TryGetValue(value, out var property))
return property.GetValue(_item, null);
throw new InvalidOperationException();
}
}
}
} |
Hello @StefH , The |
No description provided.