Пример #1
0
        private void BuildPropertyMap(Type sourceType, Type destType, MemberBinding binding, List <PropertyMap> properties, Dictionary <string, string> maps, bool buildMetadata)
        {
            var sourceMember = GetSourceMember(binding);
            var destMember   = GetDestinationMember(binding);

            if (IsConditionExpression(binding) && destMember != null)
            {
                BuildPropertyMap(destMember.Name, destMember, sourceType, destType, binding, properties, maps, buildMetadata);
                return;
            }

            if (sourceMember == null || destMember == null)
            {
                return;
            }
            var attrs = CombineAttributes(sourceMember.GetCustomAttributes(_options.InheritAttributes),
                                          destMember.GetCustomAttributes(_options.InheritAttributes), sourceMember, destMember);

            if (attrs.Any(p => p is IgnoreAttribute))
            {
                return;
            }
            var metadata = buildMetadata ? attrs.Select(p => _metadataTransformHandler.Handle(new TransformContext(p, sourceType, destType, sourceMember, destMember)))
                           .Where(data => data != null).ToList() : new List <object>(0);

            var(source, destination) = GetAssignmentInfo(binding.ToString());
            maps.Add(destination, source);
            properties.Add(new PropertyMap(sourceMember, destMember,
                                           sourceType.CreatePropertyLambda(source),
                                           metadata, null));
        }
Пример #2
0
 //
 // Summary:
 //     Visits the children of the System.Linq.Expressions.MemberBinding.
 //
 // Parameters:
 //   node:
 //     The expression to visit.
 //
 // Returns:
 //     The modified expression, if it or any subexpression was modified; otherwise,
 //     returns the original expression.
 protected override MemberBinding VisitMemberBinding(MemberBinding node)
 {
     Console.WriteLine("VisitMemberBinding:");
     Console.WriteLine('\t' + node.GetType().ToString());
     Console.WriteLine('\t' + node.ToString());
     return(base.VisitMemberBinding(node));
 }
Пример #3
0
        static void Main(string[] args)
        {
            Loader.Load();
            Objects.LoadType(typeof(DynamicLinqTestObjects));

            var lines = new List <string>();

            foreach (var(key, filename) in rendererFileMapping.SelectKVP((k, v) => (k, v)))
            {
                var ordering = parseFileOrder(@$ "C:\Users\Spitz\source\repos\zspitz\ExpressionTreeToString\Tests\expectedResults\{filename}-testdata.txt");

                var language = key == VisualBasic ? Language.VisualBasic : Language.CSharp;
                //var language = key == CSharp ? Language.CSharp : Language.VisualBasic;

                const string dlinq   = nameof(DynamicLinqTestObjects);
                var          objects = Objects.Get()
                                       //.Where(x => key == DynamicLinq ? x.source == dlinq : x.source != dlinq)
                                       .Where(x => key == DynamicLinq)
                                       .Where(x => !ordering.ContainsKey($"{x.source}.{x.name}"));

                DynamicLinqWriterVisitor.CustomAccessibleTypes.Add(typeof(Console));
                DynamicLinqWriterVisitor.CustomAccessibleTypes.Add(typeof(Dummy));

                //var objects = Objects.Get()
                //    .Where(x => key == DynamicLinq ? x.source == dlinq : x.source != dlinq)
                //    .Where(x => key == DynamicLinq)
                //    .OrderBy(x => ordering.TryGetValue($"{x.source}.{x.name}", out var order) ? order : -1);

                foreach (var(category, source, name, o) in objects)
                {
                    var toWrite = o switch
                    {
                        Expression expr => expr.ToString(key, out var pathSpans, language),
                        MemberBinding mbind => mbind.ToString(key, out var pathSpans, language),
                        ElementInit init => init.ToString(key, out var pathSpans, language),
                        SwitchCase switchCase => switchCase.ToString(key, out var pathSpans, language),
                        CatchBlock catchBlock => catchBlock.ToString(key, out var pathSpans, language),
                        LabelTarget labelTarget => labelTarget.ToString(key, out var pathSpans, language),
                        _ => throw new NotImplementedException(),
                    };
                    lines.Add($"---- {source}.{name}");
                    if (key == FactoryMethods)
                    {
                        var toReplace =
                            language == Language.CSharp ?
                            @"// using static System.Linq.Expressions.Expression

" :
                            @"' Imports System.Linq.Expressions.Expression

";
                        toWrite = toWrite.Replace(toReplace, "");
                    }
                    lines.Add(toWrite);
                }

                lines.Add("------");
            }
Пример #4
0
        static void Main(string[] args)
        {
            Loader.Load();

            var lines = new List <string>();

            foreach (var(key, filename) in rendererFileMapping)
            {
                var ordering = parseFileOrder(@$ "C:\Users\Spitz\source\repos\zspitz\ExpressionTreeToString\Tests\expectedResults\{filename}-testdata.txt");

                var language = key == VisualBasic ? Language.VisualBasic : Language.CSharp;

                //foreach (var (category, source, name, o) in Objects.Get().OrderBy(x => ordering[$"{x.source}.{x.name}"])) {
                foreach (var(category, source, name, o) in Objects.Get().Where(x => !ordering.ContainsKey($"{x.source}.{x.name}")))
                {
                    lines.Add($"---- {source}.{name}");
                    var toWrite = o switch
                    {
                        Expression expr => expr.ToString(key, out var pathSpans, language),
                        MemberBinding mbind => mbind.ToString(key, out var pathSpans, language),
                        ElementInit init => init.ToString(key, out var pathSpans, language),
                        SwitchCase switchCase => switchCase.ToString(key, out var pathSpans, language),
                        CatchBlock catchBlock => catchBlock.ToString(key, out var pathSpans, language),
                        LabelTarget labelTarget => labelTarget.ToString(key, out var pathSpans, language),
                        _ => throw new NotImplementedException(),
                    };
                    if (key == FactoryMethods)
                    {
                        toWrite = toWrite.Replace(@"// using static System.Linq.Expressions.Expression

", "");
                    }
                    lines.Add(toWrite);
                }

                lines.Add("------");
            }
Пример #5
0
 /// <summary>访问子内容为: <see cref="T:System.Linq.Expressions.MemberBinding"></see>.</summary>
 /// <param name="node">被访问的表达式</param>
 /// <returns>The modified expression, if it or any subexpression was modified; otherwise, returns the original expression.</returns>
 protected override MemberBinding VisitMemberBinding(MemberBinding node)
 {
     Log(node.ToString());
     throw new NotImplementedException();
 }
 /// <summary>
 /// Visits the children of the <see cref=" System.Linq.Expressions.MemberBinding"/>.
 /// </summary>
 /// <param name="node">The expression to visit.</param>
 /// <returns>The modified expression, if it or any subexpression was modified; otherwise, returns the original expression.</returns>
 protected override MemberBinding VisitMemberBinding(MemberBinding node)
 {
     // new a() { b = x }
     InvalidExpression(node.ToString());
     return(null);
 }
 protected static void CannotOptimize(MemberBinding binding)
 {
     throw new QueryOptimizationException(binding.ToString());
 }
        GetAssignmentFromBinding(MemberBinding binding,
                                 ParameterExpression paramSourceExisting,
                                 ParameterExpression paramSourceNew,
                                 ParameterExpression paramDestination)
        {
            var existingAssignment = binding as MemberAssignment
                                     ?? throw new ArgumentException("Only binding of type MemberAssignment can be used. " +
                                                                    $"Received binding of type {binding.BindingType}, {binding.ToString()}");

            var targetMember = Expression.MakeMemberAccess(paramDestination, existingAssignment.Member);

            var newExpression = new ParameterReplacerVisitor(paramSourceExisting, paramSourceNew)
                                .Visit(existingAssignment.Expression);

            var assignment = Expression.Assign(targetMember, newExpression);

            return(assignment);
        }
Пример #9
0
 private static void CannotConvertToSoda(MemberBinding binding)
 {
     throw new LinqQueryException(binding.ToString());
 }