Пример #1
0
                private void WriteEnumerable(IEnumerable array)
                {
                    var childObjectWalker = new ObjectWalker(
                        this.Indent, this.Level + 2, this.Builder);

                    this.WriteTypeName(array.GetType());

                    var coll = array as ICollection;

                    if (coll != null && coll.Count == 0)
                    {
                        this.Builder.Append(" []");
                        return;
                    }

                    this.WriteIndent();
                    this.Builder.Append("[");
                    this.WriteIndent();
                    foreach (var o in array)
                    {
                        this.Builder.Append(' '.Repeat(this.Indent));
                        childObjectWalker.Walk(o);
                        this.Builder.Append(",");
                        this.WriteIndent();
                    }
                    this.Builder.Append("]");
                }
Пример #2
0
        public JsonResult GetSelectedItems(string data)
        {
            // Get result
            List <object> result = ObjectWalker.GetSelectedItems <Entrant, object>(root, data, typeof(Entrant), (e) => new { data = e.Name + " " + e.Surname + " (id " + e.EntrantID + ")" });

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Пример #3
0
                private void WritePrinter(IPrint print)
                {
                    var type = print.GetType();
                    var childObjectWalker = new ObjectWalker(
                        this.Indent, this.Level + 2, this.Builder);

                    bool unAttr = type.GetTypeInfo().GetCustomAttribute <PrintAttribute>() == null;

                    this.WriteIndent();
                    this.Builder.Append("{");

                    foreach (var f in type.GetRuntimeFields().Where(f =>
                                                                    !f.IsStatic &&
                                                                    (unAttr || f.GetCustomAttribute <PrintAttribute>() != null) &&
                                                                    f.Name[0] != '<'))
                    {
                        var fieldWalker = new FieldWalker(f, childObjectWalker,
                                                          this.Indent, this.Level + 1, this.Builder);
                        fieldWalker.Walk(print);
                    }

                    foreach (var p in type.GetRuntimeProperties())
                    {
                        if (p.CanRead && (unAttr || p.GetCustomAttribute <PrintAttribute>() != null))
                        {
                            var propertyWalker = new PropertyWalker(p, childObjectWalker,
                                                                    this.Indent, this.Level + 1, this.Builder);
                            propertyWalker.Walk(print);
                        }
                    }

                    this.WriteIndent();
                    this.Builder.Append("}");
                }
Пример #4
0
        public JsonResult GetChildren(string route)
        {
            List <int> r = (List <int>) new JavaScriptSerializer().Deserialize(route, typeof(List <int>));
            // Get children
            JsonResult result = Json(ObjectWalker.GetChildrenRecursion(root, r, r.ToList(), typeof(Entrant)), JsonRequestBehavior.AllowGet);

            return(result);
        }
Пример #5
0
            public string Build()
            {
                var builder = new StringBuilder();
                var walker  = new ObjectWalker(this.indent, 0, builder);

                walker.Walk(this.obj);
                return(builder.ToString());
            }
Пример #6
0
 public PropertyWalker(PropertyInfo property, ObjectWalker valueWalker,
                       int indent, int level, StringBuilder builder)
 {
     this.property    = property;
     this.valueWalker = valueWalker;
     this.Indent      = indent;
     this.Level       = level;
     this.Builder     = builder;
 }
Пример #7
0
 public FieldWalker(FieldInfo field, ObjectWalker valueWalker,
                    int indent, int level, StringBuilder builder)
 {
     this.field       = field;
     this.valueWalker = valueWalker;
     this.Indent      = indent;
     this.Level       = level;
     this.Builder     = builder;
 }
Пример #8
0
        public void WalkObject()
        {
            ObjectWalker walker = new ObjectWalker(_order);
            int          num    = 0;

            foreach (object o in walker)
            {
                Console.WriteLine("Object #{0}: Type={1}, Value's string={2}", num++, o.GetType(), o.ToString());
            }
        }
Пример #9
0
            /// <summary>
            /// Get a list of properties and fields from a data contract that satisfy the predicate.
            /// </summary>
            protected static IEnumerable <IObjectMemberContext> GetDataMemberFields(object dataObject, Predicate <MemberInfo> memberTest)
            {
                var walker = new ObjectWalker(memberTest)
                {
                    IncludeNonPublicFields     = true,
                    IncludeNonPublicProperties = true
                };

                return(walker.Walk(dataObject));
            }
Пример #10
0
        public void OwnObjectWalkerInstance()
        {
            ParentChain   chain   = ParentChain.GetGrandFatherSample();
            StringBuilder walkLog = new StringBuilder();

            ObjectWalker <string> walker = new ObjectWalker <string>();

            walker.Options.LogToConsole = true;

            walker.GetValue(chain, "Name", s => walkLog.Append(s + ","));

            Assert.AreEqual("son,father,grandfather,greatGrandfather,", walkLog.ToString());
        }
        public void TestWalkAllMembers()
        {
            ObjectWalker walker = new ObjectWalker();

            // all members
            walker.IncludeNonPublicFields     = true;
            walker.IncludeNonPublicProperties = true;
            walker.IncludePublicFields        = true;
            walker.IncludePublicProperties    = true;

            List <IObjectMemberContext> members = new List <IObjectMemberContext>(walker.Walk(new Foo()));

            // there will be 6 members, becausing of the backing fields for the properties
            Assert.AreEqual(6, members.Count);
        }
Пример #12
0
        protected void WriteObjectProperties(object obj, XmlWriter writer)
        {
            ObjectWalker walker = new ObjectWalker(
                delegate(IObjectMemberContext ctx)
            {
                WriteProperty(ctx.Object, (PropertyInfo)ctx.Member, ctx.MemberValue, writer);
            },
                delegate(MemberInfo member)
            {
                return(AttributeUtils.HasAttribute <PersistentPropertyAttribute>(member));
            });

            walker.IncludePublicFields = false;
            walker.Walk(obj);
        }
        public void TestWalkAllMembersWithFilter()
        {
            // exclude members starting with an underscore
            ObjectWalker walker = new ObjectWalker(delegate(MemberInfo member) { return(!member.Name.StartsWith("_")); });

            // all members
            walker.IncludeNonPublicFields     = true;
            walker.IncludeNonPublicProperties = true;
            walker.IncludePublicFields        = true;
            walker.IncludePublicProperties    = true;

            List <IObjectMemberContext> members = new List <IObjectMemberContext>(walker.Walk(new Foo()));

            Assert.AreEqual(4, members.Count);
        }
Пример #14
0
		public void TestWalkPublicFields()
		{
			ObjectWalker walker = new ObjectWalker();

			// public fields
			walker.IncludeNonPublicFields = false;
			walker.IncludeNonPublicProperties = false;
			walker.IncludePublicFields = true;
			walker.IncludePublicProperties = false;

			List<IObjectMemberContext> members = new List<IObjectMemberContext>(walker.Walk(new Foo()));
			Assert.AreEqual(1, members.Count);
			Assert.AreEqual("PublicField", members[0].Member.Name);
			Assert.AreEqual("hello", members[0].MemberValue);
		}
        public void TestWalkPublicFields()
        {
            ObjectWalker walker = new ObjectWalker();

            // public fields
            walker.IncludeNonPublicFields     = false;
            walker.IncludeNonPublicProperties = false;
            walker.IncludePublicFields        = true;
            walker.IncludePublicProperties    = false;

            List <IObjectMemberContext> members = new List <IObjectMemberContext>(walker.Walk(new Foo()));

            Assert.AreEqual(1, members.Count);
            Assert.AreEqual("PublicField", members[0].Member.Name);
            Assert.AreEqual("hello", members[0].MemberValue);
        }
Пример #16
0
        protected static Dictionary <string, string> GetColumnMap(Type entityType)
        {
            ObjectWalker walker = new ObjectWalker();
            Dictionary <string, string> propMap = new Dictionary <string, string>();

            foreach (IObjectMemberContext member in walker.Walk(entityType))
            {
                EntityFieldDatabaseMappingAttribute map =
                    AttributeUtils.GetAttribute <EntityFieldDatabaseMappingAttribute>(member.Member);
                if (map != null)
                {
                    propMap.Add(member.Member.Name, map.ColumnName);
                }
            }

            return(propMap);
        }
        public void TestWalkType()
        {
            // exclude members starting with an underscore
            ObjectWalker walker = new ObjectWalker();

            // all members
            walker.IncludeNonPublicFields     = true;
            walker.IncludeNonPublicProperties = true;
            walker.IncludePublicFields        = true;
            walker.IncludePublicProperties    = true;

            // in this case we walk typeof(Foo) rather than an instance of Foo
            List <IObjectMemberContext> members = new List <IObjectMemberContext>(walker.Walk(typeof(Foo)));

            // there will be 6 members, becausing of the backing fields for the properties
            Assert.AreEqual(6, members.Count);
        }
Пример #18
0
        private static Dictionary <string, PropertyInfo> LoadMap(Type entityType)
        {
            ObjectWalker walker = new ObjectWalker();
            Dictionary <string, PropertyInfo> propMap = new Dictionary <string, PropertyInfo>();

            foreach (IObjectMemberContext member in walker.Walk(entityType))
            {
                EntityFieldDatabaseMappingAttribute map =
                    AttributeUtils.GetAttribute <EntityFieldDatabaseMappingAttribute>(member.Member);
                if (map != null)
                {
                    propMap.Add(map.ColumnName, member.Member as PropertyInfo);
                }
            }

            return(propMap);
        }
Пример #19
0
    static void Main()
    {
        // Build an object graph using an array that refers to various objects.
        Object[] data = new Object[] { "Jeff", 123, 555L, (Byte)35, new App() };

        // Construct an ObjectWalker and pass it the root of the object graph.
        ObjectWalker ow = new ObjectWalker(data);

        // Enumerate all of the objects in the graph and count the number of objects.
        Int64 num = 0;

        foreach (Object o in ow)
        {
            // Display each object's type and value as a string.
            Console.WriteLine("Object #{0}: Type={1}, Value's string={2}",
                              num++, o.GetType(), o.ToString());
        }
    }
Пример #20
0
        private void button1_Click(object sender, EventArgs e)
        {
            Configuration c = new Configuration();

            ObjectGenerator.FillObject(c, 2);

            TheTester nc = new TheTester("aaa", DateTime.Now, SomeeNum.EnumValue2);

            ObjectGenerator.FillObject(nc, 2);

            var sb = new StringBuilder();

            ObjectWalker.WalkObject(nc, new StringBuilderWalker(sb), 6);
            richTextBox1.Text = sb.ToString();

            ObjectWalker.WalkObject(nc, new TreeViewWalker(treeView1), 5);
            treeView1.ExpandAll();

            richTextBox2.Text = SerializeToXml(c);
        }
        public void TestWalkPrivateFields()
        {
            ObjectWalker walker = new ObjectWalker();

            // private field
            walker.IncludeNonPublicFields     = true;
            walker.IncludeNonPublicProperties = false;
            walker.IncludePublicFields        = false;
            walker.IncludePublicProperties    = false;

            List <IObjectMemberContext> members = new List <IObjectMemberContext>(walker.Walk(new Foo()));

            // there will be 3 private fields, becausing of the backing fields for the properties
            Assert.AreEqual(3, members.Count);
            foreach (IObjectMemberContext member in members)
            {
                if (member.Member.Name == "PrivateField")
                {
                    Assert.AreEqual(true, member.MemberValue);
                }
            }
        }
        public void TestMutation()
        {
            // exclude members starting with an underscore
            ObjectWalker walker = new ObjectWalker(delegate(MemberInfo member) { return(!member.Name.StartsWith("_")); });

            // all members
            walker.IncludeNonPublicFields     = true;
            walker.IncludeNonPublicProperties = true;
            walker.IncludePublicFields        = true;
            walker.IncludePublicProperties    = true;

            Foo foo  = new Foo();
            Foo foo2 = new Foo();

            // in this test we change all the values of foo via the IObjectMemberContext
            foreach (IObjectMemberContext context in walker.Walk(foo))
            {
                if (context.MemberType == typeof(bool))
                {
                    context.MemberValue = false;
                }
                if (context.MemberType == typeof(string))
                {
                    context.MemberValue = "goodbye";
                }
                if (context.MemberType == typeof(int))
                {
                    context.MemberValue = 1;
                }
                if (context.MemberType == typeof(object))
                {
                    context.MemberValue = foo2;
                }
            }

            Assert.AreEqual(true, foo.CheckConsistency(false, "goodbye", 1, foo2));
        }
Пример #23
0
        public virtual void Initialize()
        {
            foreach (var animation in animations.Values)
            {
                int cachedAnimations = 0;
                var curves           = new List <IAnimationCurve>(animation.Curves);
                foreach (IAnimationCurve curve in curves)
                {
                    object realTarget;

                    if (string.IsNullOrEmpty(curve.TargetName))
                    {
                        realTarget = target;
                    }
                    else
                    {
                        var resourceProvider = target as IResourceProvider;
                        if (resourceProvider != null)
                        {
                            realTarget = resourceProvider.GetResource <IResource>(curve.TargetName);
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format("'Target' does not implement {0}", typeof(IResourceProvider)));
                        }
                    }

                    if (realTarget == null)
                    {
                        throw new InvalidOperationException("'Target' cannot be null");
                    }

                    name = string.Format("{0}.Animator", ((IResource)realTarget).Name);

                    ObjectWalker walker = new ObjectWalker(realTarget, curve.TargetProperty);

                    var    requiresCaching = realTarget as IRequiresCaching;
                    string curveKey        = curve.Key;
                    if (requiresCaching != null)
                    {
                        var newCurve = requiresCaching.CacheAnimation(walker.CurrentMember.DeclaringType, walker.CurrentMember.Name, curve);
                        if (newCurve != null)
                        {
                            animation.RemoveCurve(curve.TargetProperty);
                            animation.AddCurve(newCurve);
                            walker.SetTarget(requiresCaching, newCurve.TargetProperty);
                            curveKey = newCurve.Key;
                            cachedAnimations++;
                        }
                    }

                    var animatable = walker.CurrentMember.GetCustomAttribute <AnimatableAttribute>();
                    if (animatable == null)
                    {
                        throw new InvalidOperationException(string.Format("'{0}' is not marked as {1}", walker.CurrentMember.Name,
                                                                          typeof(AnimatableAttribute).Name));
                    }

                    walkers.Add(curveKey, walker);
                }

                if (cachedAnimations > 0 && cachedAnimations != curves.Count)
                {
                    throw new InvalidOperationException(string.Format("Mixing cached and uncached animations is not supported"));
                }
            }
        }
Пример #24
0
 public virtual void Setup()
 {
     ObjectWalkerDefault = new ObjectWalker <Object>();
     ObjectComparer      = new ObjectComparer();
 }
Пример #25
0
		public void TestMutation()
		{
			// exclude members starting with an underscore
			ObjectWalker walker = new ObjectWalker(delegate(MemberInfo member) { return !member.Name.StartsWith("_"); });

			// all members
			walker.IncludeNonPublicFields = true;
			walker.IncludeNonPublicProperties = true;
			walker.IncludePublicFields = true;
			walker.IncludePublicProperties = true;

			Foo foo = new Foo();
			Foo foo2 = new Foo();

			// in this test we change all the values of foo via the IObjectMemberContext
			foreach (IObjectMemberContext context in walker.Walk(foo))
			{
				if (context.MemberType == typeof(bool))
					context.MemberValue = false;
				if (context.MemberType == typeof(string))
					context.MemberValue = "goodbye";
				if (context.MemberType == typeof(int))
					context.MemberValue = 1;
				if (context.MemberType == typeof(object))
					context.MemberValue = foo2;

			}

			Assert.AreEqual(true, foo.CheckConsistency(false, "goodbye", 1, foo2));
		}
Пример #26
0
		public void TestWalkType()
		{
			// exclude members starting with an underscore
			ObjectWalker walker = new ObjectWalker();

			// all members
			walker.IncludeNonPublicFields = true;
			walker.IncludeNonPublicProperties = true;
			walker.IncludePublicFields = true;
			walker.IncludePublicProperties = true;

			// in this case we walk typeof(Foo) rather than an instance of Foo
			List<IObjectMemberContext> members = new List<IObjectMemberContext>(walker.Walk(typeof(Foo)));
			// there will be 6 members, becausing of the backing fields for the properties
			Assert.AreEqual(6, members.Count);
		}
Пример #27
0
		public void TestWalkAllMembersWithFilter()
		{
			// exclude members starting with an underscore
			ObjectWalker walker = new ObjectWalker(delegate(MemberInfo member) { return !member.Name.StartsWith("_"); });

			// all members
			walker.IncludeNonPublicFields = true;
			walker.IncludeNonPublicProperties = true;
			walker.IncludePublicFields = true;
			walker.IncludePublicProperties = true;

			List<IObjectMemberContext> members = new List<IObjectMemberContext>(walker.Walk(new Foo()));
			Assert.AreEqual(4, members.Count);
		}
Пример #28
0
		public void TestWalkAllMembers()
		{
			ObjectWalker walker = new ObjectWalker();

			// all members
			walker.IncludeNonPublicFields = true;
			walker.IncludeNonPublicProperties = true;
			walker.IncludePublicFields = true;
			walker.IncludePublicProperties = true;

			List<IObjectMemberContext> members = new List<IObjectMemberContext>(walker.Walk(new Foo()));
			// there will be 6 members, becausing of the backing fields for the properties
			Assert.AreEqual(6, members.Count);
		}
Пример #29
0
		public void TestWalkPrivateFields()
		{
			ObjectWalker walker = new ObjectWalker();

			// private field
			walker.IncludeNonPublicFields = true;
			walker.IncludeNonPublicProperties = false;
			walker.IncludePublicFields = false;
			walker.IncludePublicProperties = false;

			List<IObjectMemberContext> members = new List<IObjectMemberContext>(walker.Walk(new Foo()));
			// there will be 3 private fields, becausing of the backing fields for the properties
			Assert.AreEqual(3, members.Count);
			foreach (IObjectMemberContext member in members)
			{
				if(member.Member.Name == "PrivateField")
					Assert.AreEqual(true, member.MemberValue);
			}
			
		}
Пример #30
0
 public T Attach <T>(T obj) where T : IOgmEntity
 {
     return((T)ObjectWalker.Visit(obj));
 }