Exemplo n.º 1
0
        public void CreateTreeProperly()
        {
            var first = new ClassWithRecursion();

            first.Name = "First";
            var second = new ClassWithRecursion();

            second.Name = "Second";

            first.Subclass = second;

            var sourceTree = new ObjectTreeItem(first);

            sourceTree.CreateTree();
            Trace.WriteLine(sourceTree.ToFormattedString());
            Assert.IsNull(sourceTree.Parent);
            Assert.IsNotNull(sourceTree.Childs);

            Assert.IsTrue(IEnumerableExtensions.Any(sourceTree.Childs));
            Assert.AreEqual(2, sourceTree.Childs.Count);

            Assert.AreEqual(first, sourceTree.Item);
            Assert.IsTrue(sourceTree.Childs.Any(item => item.Item == second));

            foreach (var objectTreeItem in sourceTree.Childs)
            {
                Assert.AreEqual(sourceTree, objectTreeItem.Parent);
            }
        }
Exemplo n.º 2
0
        public static string StringValue(object o, string language)
        {
            var(isLiteral, repr) = TryRenderLiteral(o, language);
            if (!isLiteral)
            {
                var hasDeclaredToString = o.GetType().GetMethods().Any(x =>
                {
                    if (x.Name != "ToString")
                    {
                        return(false);
                    }
                    if (IEnumerableExtensions.Any(x.GetParameters()))
                    {
                        return(false);
                    }
                    if (x.DeclaringType == typeof(object))
                    {
                        return(false);
                    }
                    if (x.DeclaringType.InheritsFromOrImplements <EnumerableQuery>())
                    {
                        return(false);
                    }
                    return(true);
                });

                if (hasDeclaredToString)
                {
                    return(o.ToString());
                }
            }

            return(repr);
        }
Exemplo n.º 3
0
        public static MethodInfo GetMethod(Expression <Action> expr, params Type[] typeargs)
        {
            var ret = ((MethodCallExpression)expr.Body).Method;

            // TODO handle partially open generic methods
            if (IEnumerableExtensions.Any(typeargs) && ret.IsGenericMethod)
            {
                ret = ret.GetGenericMethodDefinition().MakeGenericMethod(typeargs);
            }
            return(ret);
        }
Exemplo n.º 4
0
        public static object[] TupleValues(object tuple)
        {
            if (!tuple.GetType().IsTupleType())
            {
                throw new InvalidOperationException();
            }
            var fields = tuple.GetType().GetFields();

            if (IEnumerableExtensions.Any(fields))
            {
                return(tuple.GetType().GetFields().Select(x => x.GetValue(tuple)).ToArray());
            }
            return(tuple.GetType().GetProperties().Select(x => x.GetValue(tuple)).ToArray());
        }
Exemplo n.º 5
0
 public void Any_throws_ArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => IEnumerableExtensions.Any(null));
 }