Sunday, June 7, 2009

PyDictionary

Very simple (not complete) Python-style dictionary in .NET 4.0 using dynamic objects. Allows for arbitrary members to be created and used on the fly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace TestApplication
{
class PyDictionary : DynamicObject
{
private Dictionary<string, object> m_state =
new Dictionary<string,object>();

public override IEnumerable<string> GetDynamicMemberNames()
{
return m_state.Keys;
}

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

public override bool TryGetMember(GetMemberBinder binder,
out object result)
{
result = m_state[binder.Name];
return true;
}
};

class Program
{
static void Main(string[] args)
{
dynamic baby = new PyDictionary();

baby.Name = "Alyssa";
baby.Age = 0.8f;

Console.WriteLine(baby.Name + " is " + baby.Age + " years old!");

// I will throw a KeyNotFoundException
int iq = baby.IQ;
}
}

Saturday, June 6, 2009

Downloaded Visual Studio 2010 Beta

So I just downloaded Visual Studio 2010 and I have to say first impressions are good.  The start page looks much better than before, but especially because you can customize it.  I doubt I'll ever have the time to delve into doing that, but I can definitely see some creative people coming up with some good stuff there.

The first thing I did was start a C# project and do the following

dynamic name = "Chris";
dynamic age = 23.45;
dynamic str =  name.ToUpper() + " " + age;

It worked!!  I started trying to create my own custom type derived from IDynamicObject, but couldn't find what namespace it was in.  (Anyone know?)  I was going to try and implement a Python style dictionary type so I could do something like:

dynamic grades = new GradeBook();
grades.Chris = 98.4;
grades.David = 456.2;
float classAvg = grades.ClassAverage();

But I suppose that will have to wait.  Not to mention that just looks odd in C#.

I created a C++ project next and found that there was no .ncb file anymore! There also is no .vcproj file anymore either, but there is:
  • sln - Same strange and inflexible format as before
  • vcxproj - New msbuild compatible file for build settings only
  • vcxproj.filters - msbuild file containing only the included file
  • vcxproj.user - User settings in XML format this time (!)
Finally!, msbuild integrated and an actual seperation between compilation options and the physical contents in a project.  Now if only Solution Files did the same thing...  but I suppose that would be asking for too much :)

More later.

Saturday, December 6, 2008

First post.  That was easy.