IgorShare Thoughts and Ideas

Consulting and Training

Archive for the ‘Reflection’ Category

Interactive Data Dependency Visualizations

Posted by Igor Moochnick on 03/01/2012

There is nothing aids better in understanding your data than a great visualization. Today there is an abundance of different techniques and tools, but today I’d like to show what is readily available for you in .Net.

Let’s take a simple example of understanding dependencies between your assemblies.

If you’re running in a Visual Studio and you have a full access to the source code, it is possible to see the referenced assemblies but it’s pretty hard to track what assembly depend on the assembly you’re working on. [Note: if you’re really in need of a ready made tool for this purpose, you can check the “Architecture Tools” for Visual Studio 2010 or NDepend tool]

A visualization like this is priceless:

image

On this graph you can see a graph of the assemblies dependencies for the Paint.Net. The snapshot above highlights the dependencies of the PaingDotNet.SystemLayer (highlighted in blue) by showing all the assemblies it depends on in “yellow” and all the assemblies dependent on it in “green”. The rest is left in a “gray” color.

Let’s see how you can create interactive dependency graphs for your data.

In my case I chose to use the QuickGraph module from CodePlex that can be download from NuGet [PM> Install-Package QuickGraph]. It provides a very simple yet robust API that allows you to create the directional and non-directional graphs in memory and then save them in different formats.

Here is a small example of how to create 2 vertices (V1 and V2) and a directional edge (from V1 to V2):

var graph = new AdjacencyGraph();
graph.AddVertex("v1");
graph.AddVertex("v2");
var edge = new TaggedEdge("v1", "v2", "depends");
graph.AddEdge(edge); 

As soon as all the graph is created it can be serialized, for example, in a GraphML standard format like this:

using(var xwriter = XmlWriter.Create(...))
    g.SerializeToGraphML(xwriter);

This is it. GraphML files can be opened by a range of different tools, but, if you’d like to show these visualizations in your WPF applications you can use a Graph# from CodePlex.

Here is a sample WPF application rendering a GraphML dependency graph:

image

As you can see, you don’t have to be afraid of interactive data visualizations but you can make your applications shine and provide useful and easily digestible information for your users.

Note: Source code for this article you can download …here from the Git repo…

Posted in C#, Reflection | 4 Comments »

How to find all Workflows in an assembly

Posted by Igor Moochnick on 01/28/2008

This is a beginning of series of articles on workflows discoverablity and automation.

It’s easy to start a workflow if you know it’s type:

   1: WorkflowRuntime runtime = new WorkflowRuntime();
   2: runtime.StartRuntime();
   3: 
   4: WorkflowInstance workflow = runtime.CreateWorkflow(typeof(MyWorkflowType), parms);

But what if you have an assembly and you don’t know a workflow type? Reflection is the answer. This is how you can find all the types in an assembly that inherit from an Activity (base type for all workflows) type:

   1: public static IEnumerable<Type> FindWorkflows(Assembly asm)
   2: {
   3:     return from t in asm.GetTypes()
   4:            where t.IsSubclassOf(typeof(Activity))
   5:            select t;
   6: }
   7: 
   8: [TestMethod]
   9: public void FindAllWorkflowsInAssembly()
  10: {
  11:     foreach (Type t in WFHelper.FindWorkflows(WFHelper.LoadWorkflowAssembly(assembly_path)))
  12:     {
  13:         System.Diagnostics.Trace.WriteLine(t.Name);
  14:     }
  15: }

Note: the list, returned by FindWorfklows function, will contain all Custom activities in the provided assembly as well.

If you want to know how to narrow down the list, stay tuned.

Posted in C#, Reflection, Tutorials, Workflows | Leave a Comment »

Dynamic code generation

Posted by Igor Moochnick on 01/12/2008

To add to my previous post CodeDom extensions and dynamic LINQ. I’d like to mentions that the same technique can be used to generate/emit a valid compliable code.

Fore example the following snippet:

   1: var c = new CodeDom();
   2: c.AddNamespace("Samples").Imports("System")
   3:     .AddClass(
   4:       c.Class("TestClass")
   5:         .AddMethod(c.Method("PrintTest", "Console.WriteLine(\"Hello world !\");")));
   6: 
   7: string s = c.GenerateCode();

will generate the following code, which, at the end, can be written to a file or compiled in-memory:

   1: //------------------------------------------------------------------------------
   2: // <auto-generated>
   3: //     This code was generated by a tool.
   4: //     Runtime Version:2.0.50727.1433
   5: //
   6: //     Changes to this file may cause incorrect behavior and will be lost if
   7: //     the code is regenerated.
   8: // </auto-generated>
   9: //------------------------------------------------------------------------------
  10: 
  11: namespace Samples {
  12:     using System;
  13:
  14:
  15:     public class TestClass
  16:     {
  17:         public static void PrintTest()
  18:         {
  19:             Console.WriteLine(\"Hello world!\");
  20:         }
  21:     }
  22: }

So, unleash your imagination!

[The full source code with tests/examples you can find, as usual, on my web site: http://igor.moochnick.googlepages.com]

Posted in C#, Reflection, Tutorials | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: