Developer's Perception

Odd thoughts from a developer.

Playing With the Dynamic Keyword

| Comments

I have not yet had a chance to play around with the dynamic features that was introduced with .NET 4, probably because I have yet to find a good scenario for using it and probably because I am mostly stuck on doing work on the .NET 3.5.

Fortunately, I am currently working on a new REST API build in Nancy and the dynamic features of .NET 4 seems perfect to implement HTTP PATCH.

Our way of thinking is that it is hard to model with traditional classes as not all properties are necessarily included. One solution could be to make them all nullable and use that, but then it would not be possible for us to distinguish between a property simply not in the body of the request and a property the consumer deliberately set to null.

My first test was like this:

1
2
3
4
5
public void DynamicValueNoneSet()
{
  dynamic a = new ExpandoObject();
  string name = a.Name;
}

This showed me that I would get a RunTimeBinderException if a given value was not available (yes I could also have read the documentation, but how much fun is that?).

My next attempt included this little implementation of DynamicObject – to avoid getting the exception when using it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class NullDynamic : DynamicObject
{
  private readonly IDictionary<string, object> expando;
  
  public NullDynamic()
  {
      expando = new ExpandoObject();
  }

  public override bool TryGetMember(GetMemberBinder binder, out object result)
  {
      expando.TryGetValue(binder.Name, out result);
      return true;
  }

  public override bool TrySetMember(SetMemberBinder binder, object value)
  {
      expando[binder.Name] = value;
      return true;
  }
}

Since the declared goal is to be able to distinguish between a value not being there and deliberately being set to null, this would not work either.

Actually what I found myself thinking was that I missed the “undefined” value from javascript, or some other way to express that something was not there, built into the language.

Trying to avoid going to such drastic measure I came up with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Dynosaur: DynamicObject
{
  private readonly IDictionary<string, object> expando;

  public Dynosaur()
  {
      expando = new ExpandoObject();
  }

  public override bool TryGetMember(GetMemberBinder binder, out object result)
  {
      return expando.TryGetValue(binder.Name, out result);
  }

  public override bool TrySetMember(SetMemberBinder binder, object value)
  {
      expando[binder.Name] = value;
      return true;
  }

  public bool HasValue(string name)
  {
      return expando.Keys.Contains(name);
  }
}

Maybe not the most elegant solution, but I does allow me to write code like:

1
2
3
4
5
6
7
8
public void PrintName(dynamic a)
{
  if (!a.HasValue("Name"))
  {
      a.Name = "dog";
  }
  Console.WriteLine("the name is: " + a.Name);
}

Given that “a” is actually an instance of my Dynosaur class. I would not say that I am entirely satisfied with this implementation, but I actually think that this is as far as the dynamic features of C# will take me.

Any good ideas out there? I am thinking there must be other people that has played around with this and has come up some crazy dynamic stuff in C#.

Comments