Object Watch for .NET

This is an experimental implementation of a Watch to evaluate .NET objects in Runtime using VB.NET. One of the key feature of this object watch is that you can even evaluate Lambda Expressions which is not supported in the current(2012) or previous versions of visual studio. That said, most of these features are still at its infancy. Currently the watch supports member access using properties including indexer properties, method invocations and also supports LINQ Expressions. The result are serialized to JSON with the help of Newtonsoft’s JSON.NET

For the purpose of demonstration, we have a collection of objects (Students) attached to a Web Cache. This is done so the Cache can persist the collection(s) in the memory.

Dim students As New List(Of Student)
students.Add(New Student With {.Name = "Tony", .Age = 27, .Batch = 1})
students.Add(New Student With {.Name = "Peter", .Age = 24, .Batch = 2})
students.Add(New Student With {.Name = "James", .Age = 28, .Batch = 1})
students.Add(New Student With {.Name = "Adam", .Age = 27, .Batch = 2})
students.Add(New Student With {.Name = "Amit", .Age = 19, .Batch = 1})
students.Add(New Student With {.Name = "Rene", .Age = 45, .Batch = 2})
students.Add(New Student With {.Name = "Ash", .Age = 35, .Batch = 1})
students.Add(New Student With {.Name = "John", .Age = 20, .Batch = 2})
students.Add(New Student With {.Name = "Rich", .Age = 27, .Batch = 1})
 'Now convert the list to a dictionary.
 Dim students2 As Dictionary(Of String, Student) = students.ToDictionary(Function(o) o.Name)

Now let’s start querying.

Property Access

Gets a simple property. The query syntax starts with a dot (“.”) The dictionary students2, has few properties, for the purpose of demonstration we will be using two prominent properties Keys and Values

Indexer Properties

As indexer takes in property parameters, the query takes parameters similar to indexer properties in .NET

Method Calls

Similar to how a method is called in .NET, method’s starts with a “.”, followed by the name as the arguments. Syntax is similar to how methods are called in .NET

Enumerable Extension methods

Probably one of the key feature of this watch is it’s ability to query collection of objects using Lambda expressions. At the moment, there are support for 3 key Enumerable functions which are Where, Select and Take.

Enumerable.Take method

Enumerable.Where method

Where method takes a predicate, firstly parsed and converted to expression lambda and then it used to evaluate each item in the collection.

Enumerable.Select method

Similar to .Where, instead it project a single element of each item in a collection.

Fluent Style

One of the key demand of a watch is to evaluate set of statements/expression fluently. Which implies you can query sequentially in any combination provided the query follows syntax and semantics of VB. In the example below, the first query uses a property which returns a value collection, and then that result set is evaluated with a predicate yielding a new result set.

The motive of this project is to demonstrate few tricks and black arts in watching objects at run time and runtime method executions. Using the code is completely at the users risk. Therefore, contributors of this project WILL NOT be liable for any damages caused.

The project is available at : https://github.com/tonyjose/DotNetObjectWatch

Create a free website or blog at WordPress.com.

Up ↑