From e6181c24124d97f2fbc932b8a68311e625463156 Mon Sep 17 00:00:00 2001 From: uzvkl Date: Tue, 11 Jun 2019 23:05:52 +0200 Subject: Move dsa related stuff to subfolder --- .../Offline/Internals/MemberAccessVisitor.cs | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs (limited to 'dsa/FireBase/Offline/Internals') diff --git a/dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs b/dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs new file mode 100644 index 0000000..89a77da --- /dev/null +++ b/dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using Newtonsoft.Json; + +namespace Firebase.Database.Offline.Internals +{ + public class MemberAccessVisitor : ExpressionVisitor + { + private readonly IList propertyNames = new List(); + + private bool wasDictionaryAccess; + + public IEnumerable PropertyNames => propertyNames; + + public override Expression Visit(Expression expr) + { + if (expr?.NodeType == ExpressionType.MemberAccess) + { + if (wasDictionaryAccess) + { + wasDictionaryAccess = false; + } + else + { + var memberExpr = (MemberExpression) expr; + var jsonAttr = memberExpr.Member.GetCustomAttribute(); + + propertyNames.Add(jsonAttr?.PropertyName ?? memberExpr.Member.Name); + } + } + else if (expr?.NodeType == ExpressionType.Call) + { + var callExpr = (MethodCallExpression) expr; + if (callExpr.Method.Name == "get_Item" && callExpr.Arguments.Count == 1) + { + var e = Expression.Lambda(callExpr.Arguments[0]).Compile(); + propertyNames.Add(e.DynamicInvoke().ToString()); + wasDictionaryAccess = callExpr.Arguments[0].NodeType == ExpressionType.MemberAccess; + } + } + + return base.Visit(expr); + } + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2