//
        private void BindMemberOrder( )
        {
            IList <EnumDescription> arrMemberOrder = MemberOrderHelper.GetMemberOrderStatusList(typeof(MemberOrderStatus));

            ckbMemberOrder.DataSource     = arrMemberOrder;
            ckbMemberOrder.DataValueField = "EnumValue";
            ckbMemberOrder.DataTextField  = "Description";
            ckbMemberOrder.DataBind( );
        }
示例#2
0
        //会员等级
        private void BindMemberOrder( )
        {
            IList <EnumDescription> arrMemberOrder = MemberOrderHelper.GetMemberOrderStatusList(typeof(MemberOrderStatus));

            ddlMemberType.DataSource     = arrMemberOrder;
            ddlMemberType.DataValueField = "EnumValue";
            ddlMemberType.DataTextField  = "Description";
            ddlMemberType.DataBind( );
            ddlMemberType.Items.RemoveAt(0);
        }
        private static SyntaxNode MoveField(FieldDeclarationSyntax fieldDeclaration, TypeDeclarationSyntax typeDeclarationNode, SyntaxNode syntaxRoot)
        {
            var fieldToMove = new MemberOrderHelper(fieldDeclaration);
            foreach (var member in typeDeclarationNode.Members)
            {
                if (!member.IsKind(SyntaxKind.FieldDeclaration))
                {
                    continue;
                }

                var orderHelper = new MemberOrderHelper(member);
                if (orderHelper.ModifierPriority < fieldToMove.ModifierPriority)
                {
                    syntaxRoot = MoveField(syntaxRoot, fieldToMove.Member, member);
                    break;
                }
            }

            return syntaxRoot;
        }
        private static SyntaxNode UpdateSyntaxRoot(MemberDeclarationSyntax memberDeclaration, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
        {
            var parentDeclaration = memberDeclaration.Parent;
            var memberToMove = new MemberOrderHelper(memberDeclaration, checks);

            if (parentDeclaration is TypeDeclarationSyntax)
            {
                return HandleTypeDeclaration(memberToMove, (TypeDeclarationSyntax)parentDeclaration, checks, syntaxRoot, indentationOptions);
            }

            if (parentDeclaration is NamespaceDeclarationSyntax)
            {
                return HandleNamespaceDeclaration(memberToMove, (NamespaceDeclarationSyntax)parentDeclaration, checks, syntaxRoot, indentationOptions);
            }

            if (parentDeclaration is CompilationUnitSyntax)
            {
                return HandleCompilationUnitDeclaration(memberToMove, (CompilationUnitSyntax)parentDeclaration, checks, syntaxRoot, indentationOptions);
            }

            return syntaxRoot;
        }
        private static SyntaxNode OrderMember(MemberOrderHelper memberOrder, SyntaxList<MemberDeclarationSyntax> members, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
        {
            var memberIndex = members.IndexOf(memberOrder.Member);
            MemberOrderHelper target = default(MemberOrderHelper);

            for (var i = memberIndex - 1; i >= 0; --i)
            {
                var orderHelper = new MemberOrderHelper(members[i], checks);
                if (orderHelper.Priority < memberOrder.Priority)
                {
                    target = orderHelper;
                }
                else
                {
                    break;
                }
            }

            return target.Member != null ? MoveMember(syntaxRoot, memberOrder.Member, target.Member, indentationOptions) : syntaxRoot;
        }
 private static SyntaxNode HandleNamespaceDeclaration(MemberOrderHelper memberOrder, NamespaceDeclarationSyntax namespaceDeclaration, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
 {
     return OrderMember(memberOrder, namespaceDeclaration.Members, checks, syntaxRoot, indentationOptions);
 }
 private static SyntaxNode HandleCompilationUnitDeclaration(MemberOrderHelper memberOrder, CompilationUnitSyntax compilationUnitDeclaration, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
 {
     return OrderMember(memberOrder, compilationUnitDeclaration.Members, checks, syntaxRoot, indentationOptions);
 }
 private static SyntaxNode HandleTypeDeclaration(MemberOrderHelper memberOrder, TypeDeclarationSyntax typeDeclarationNode, ElementOrderingChecks checks, SyntaxNode syntaxRoot, IndentationOptions indentationOptions)
 {
     return OrderMember(memberOrder, typeDeclarationNode.Members, checks, syntaxRoot, indentationOptions);
 }
        public void Random()
        {
            var random = new Random(2020_05_19);

            // all ordered
            for (var i = 0; i < 10_000; i++)
            {
                var items  = new List <(int Order, string Value)>();
                var length = random.Next(20) + 1;
                var helper = MemberOrderHelper <string> .Create();

                for (var j = 0; j < length; j++)
                {
                    var val  = random.Next();
                    var item = (Order : val, Value : val + "__");

                    items.Add(item);

                    helper.Add(item.Order, item.Value);
                }

                items.Sort((a, b) => a.Order.CompareTo(b.Order));

                var expected = string.Join(", ", items.Select(t => t.Value));
                var got      = string.Join(", ", helper.Select(t => t));

                Assert.Equal(expected, got);
            }

            // none ordered
            for (var i = 0; i < 10_000; i++)
            {
                var items  = new List <string>();
                var length = random.Next(20) + 1;
                var helper = MemberOrderHelper <string> .Create();

                for (var j = 0; j < length; j++)
                {
                    var item = j + "__";

                    items.Add(item);

                    helper.Add(null, item);
                }

                var expected = string.Join(", ", items);
                var got      = string.Join(", ", helper.Select(t => t));

                Assert.Equal(expected, got);
            }

            // some ordered
            foreach (var percUnordered in new[] { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 })
            {
                for (var i = 0; i < 1_000; i++)
                {
                    var items  = new List <(int?Order, string Value)>();
                    var length = random.Next(20) + 1;
                    var helper = MemberOrderHelper <string> .Create();

                    for (var j = 0; j < length; j++)
                    {
                        var hasOrder = random.NextDouble() > percUnordered;
                        var order    = hasOrder ? random.Next() : default(int?);

                        var val = order != null ? order + "__" : "null__" + j;

                        var item = (Order : order, Value : val);

                        items.Add(item);

                        helper.Add(item.Order, item.Value);
                    }

                    items.Sort(
                        (a, b) =>
                    {
                        if (a.Order == null)
                        {
                            if (b.Order == null)
                            {
                                return(CompareNulls(a.Value, b.Value));
                            }

                            return(1);
                        }
                        else
                        {
                            if (b.Order == null)
                            {
                                return(-1);
                            }

                            return(a.Order.Value.CompareTo(b.Order.Value));
                        }
                    }
                        );

                    var expected = string.Join(", ", items.Select(t => t.Value));
                    var got      = string.Join(", ", helper.Select(t => t));

                    Assert.Equal(expected, got);
                }
        public void Simple()
        {
            // single elements
            {
                // no order
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(null, "foo");

                    Assert.Collection(
                        inst,
                        a =>
                    {
                        Assert.Equal("foo", a);
                    }
                        );
                }

                // negative
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(-1, "foo");

                    Assert.Collection(
                        inst,
                        a =>
                    {
                        Assert.Equal("foo", a);
                    }
                        );
                }

                // positive
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(1, "foo");

                    Assert.Collection(
                        inst,
                        a =>
                    {
                        Assert.Equal("foo", a);
                    }
                        );
                }
            }

            // two elements
            {
                // no order
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(null, "foo");
                    inst.Add(null, "bar");

                    Assert.Collection(
                        inst,
                        a => Assert.Equal("foo", a),
                        a => Assert.Equal("bar", a)
                        );
                }

                // 1 ordered
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(1, "foo");
                    inst.Add(null, "bar");

                    Assert.Collection(
                        inst,
                        a => Assert.Equal("foo", a),
                        a => Assert.Equal("bar", a)
                        );
                }

                // 1 ordered (reverse)
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(null, "foo");
                    inst.Add(-1, "bar");

                    Assert.Collection(
                        inst,
                        a => Assert.Equal("bar", a),
                        a => Assert.Equal("foo", a)
                        );
                }

                // both ordered
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(-2, "foo");
                    inst.Add(-1, "bar");

                    Assert.Collection(
                        inst,
                        a => Assert.Equal("foo", a),
                        a => Assert.Equal("bar", a)
                        );
                }

                // both ordered (reversed)
                {
                    var inst = MemberOrderHelper <string> .Create();

                    inst.Add(2, "foo");
                    inst.Add(1, "bar");

                    Assert.Collection(
                        inst,
                        a => Assert.Equal("bar", a),
                        a => Assert.Equal("foo", a)
                        );
                }
            }
        }