Conversation
hazzik
reviewed
Feb 9, 2021
Collaborator
Author
|
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. |
Contributor
|
@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();
}
}
}
} |
Member
|
Hello @StefH , The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.