Author Topic: C# 4.0 sample  (Read 1900 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8799
  • AKA Daniel
C# 4.0 sample
« on: March 19, 2010, 12:47:11 PM »
and Bricscad

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

using BricscadApp;
using BricscadDb;
using RxNet.ApplicationServices;
using RxNet.DatabaseServices;
using RxNet.Runtime;
using RxNet.Geometry;
using RxNet.RxGlobal;

namespace Bricscad
{
  public class DynamicDictionary : DynamicObject
  {
    Dictionary<string, object>
      _dictionary = new Dictionary<string, object>();

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

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

  public static class Commands
  {
    [CommandMethod("doit")]
    public static void Doit()
    {
      dynamic dict = new DynamicDictionary();
      dict.Foo = "Some Value";
      dict.Bar = 123;
      dict.Beer = "beer";

      GlobalFunctions.Printf("Foo: {0}, Bar: {1} , Beer: {2}", dict.Foo, dict.Bar, dict.Beer);
    }
  }
}

« Last Edit: March 19, 2010, 12:52:36 PM by Daniel »

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: C# 4.0 sample
« Reply #1 on: March 23, 2010, 06:48:45 PM »
The DynamicObject is exactly what I needed just today.
Oh well.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions