Standing on the shoulders of giants. RSS 2.0
# Saturday, May 30, 2009

The new dynamic keyword and the DLR in C# 4 and the .NET Framework 4.0 can be used for good, as displayed by IronRuby, IronPython and several other samples.

But it’s much more to fun to use it for evil… by making Reflection simple.

In C# 3.0 invoking members through reflection, was kind of odd and certainly not very readable.

var employee = new Employee();
var members = employee.GetType().GetMember("age", MemberTypes.All,
                BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

((FieldInfo)members[0]).SetValue(employee, 30);

By wrapping all reflection magic in a dynamic object the same call would look like:

var employee = (new Employee()).AsDynamic();

employee.Name = "Paul van Brenk";
employee.Age = 30;

Console.WriteLine("Employee {0} is {1} years old.", employee.Name, employee.Age);

How this works is relatively easy.. by deriving a wrapper class from the new DynamicObject and overriding the TrySetMember and TryGetMember object to do the dirty work for you.

static class DynamicHelper
{
    public static dynamic AsDynamic(this T source)
    {
        return new DynamicReflection(source);
    }

    class DynamicReflection : DynamicObject
    {
        public DynamicReflection(T source)
            : base()
        {
            this.Source = source;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            // find the member
            MemberInfo member;
            if (!TryFindMember(binder.Name, out member))
            {
                return false;
            }

            // we can only set values to fields and properties
            // using reflection
            switch (member.MemberType)
            {
                /* todo: check the type of the incoming value and the type of 
                  the property. */
                case (MemberTypes.Field):
                    ((FieldInfo)member).SetValue(Source, value);
                    return true;
                case (MemberTypes.Property):
                    ((PropertyInfo)member).SetValue(Source, value,/*ndex*/ null); // we don't support indexed properties
                    return true;
            }

            // didn't work
            return false;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            // find the member
            MemberInfo member;
            if (!TryFindMember(binder.Name, out member))
            {
                result = null;
                return false;
            }

            // we can only set values to fields and properties
            // using reflection
            switch (member.MemberType)
            {
                /* todo: check the type of the incoming value and the type of 
                  the property. */
                case (MemberTypes.Field):
                    result = ((FieldInfo)member).GetValue(Source);
                    return true;
                case (MemberTypes.Property):
                    result = ((PropertyInfo)member).GetValue(Source,/*ndex*/ null); // we don't support indexed properties
                    return true;
            }

            // didn't work
            result = null;
            return false;
        }

        private bool TryFindMember(string name, out MemberInfo memberInfo)
        {
            // find the member
            var members = Type.GetMember(name, MemberTypes.All,
                                      BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            // more than 1 not supported for now
            if (members.Length != 1) { memberInfo = null; return false; }

            memberInfo = members[0];
            return true;
        }

        private Type Type { get { return typeof(T); } }

        public T Source { get; private set; }
    }
}

More info about implementing dynamic objects and behavior can be found on the DLR CodePlex site (esp. Getting Started with the DLR as a Library Author).

Source: Program.cs.txt

Saturday, May 30, 2009 7:53:08 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
C# | Visual Studio 2010
# Wednesday, April 08, 2009

Clemens has posted a number of interesting posts about the .NET Services March 2009 CTP and SDK.

Introduction and new features

Queues and Service Bus Routers
Both are new for this release and look very promising for a number of (reliable) messaging scenarios between parties separated with various NAT and firewall devices.

Miscellaneous

To get a great overview of all these posts you can also watch the recording of his Mix09 session.

This is an ongoing series, so I’ll update this post when he posts a new article. (Or you can just subscribe on his site.)

Wednesday, April 08, 2009 7:14:36 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
Services
# Friday, April 03, 2009

412yJeVFALL[1] Today I finally finished reading Concurrent Programming on Windows by Joe Duffy. It took me this long, not because it’s a boring book (it’s not!), but because it covers a lot of ground and a goes into a lot depth.

This is not a book for someone looking for a 3-step program to start writing flawless concurrent programs, simply because there is no such program. As Joe points out throughout the book, writing concurrent programs is hard and error prone. It’s definitely not a simple case of running running all the items in your for or foreach loop on a new thread and automagically your program got a n-times faster.

Amount of subjects the book covers easily explains it’s size (900 pages), it covers a wide range of subjects from low level Memory models, via threads and lock object, to concurrent containers and finally to Parallel Containers for .NET CTP. I think this is a must read book for all serious developers, who in the near or not so near future expect to be writing concurrent programs. Considering the future of CPU’s and cloudcomputing, I believe that will be all of us.

Friday, April 03, 2009 8:58:48 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
Reading
# Wednesday, March 18, 2009

Announced today: the Microsoft Web Application Gallery. Combined with the WebPI 2.0 it's a single click experience for installing popular and free applications on IIS, where the tool takes care of all dependencies.

For example use this button to install the most recent (2.3) version of dasBlog:
Install DasBlog now with the Web Platform Installer

More info @ScottHanselman.

Wednesday, March 18, 2009 5:20:31 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
dasBlog
# Monday, March 16, 2009

In anticipation of a big announcement on Wednesday we decided to do an interim release of dasBlog 2.3.9074.18820. This is a service release containing only bugfixes. Until Wednesday you can get this release on the daily build page (scroll to the bottom) or on CodePlex, after Wednesday there is an additional distribution and installation method.

Monday, March 16, 2009 9:38:40 AM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
dasBlog
# Saturday, February 28, 2009

image

It doesn’t let you save anything, Windows Azure doesn’t like it when you try to write to the filesystem, but it shows the posts & loads the theme. And with only minor changes!

I used the manual from “Cloudy in Seattle” to get an existing ASP.NET app running on Windows Azure, to get started. Because ASP.NET is an older application we still had a referce to MSBuild 8.0 in the proj. file and the Azure packaging tool didn’t like that so I removed the following from the web project.

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v8.0\WebApplications\Microsoft.WebApplication.targets" 
Condition=" '$(Solutions.VSVersion)' == '8.0'" />

Further I added a dummy log file to the logs directory to make sure that directory was avaialble got created. And made sure all files which are required have the Build action set to content:

image

The final thing to do is make sure you use the IIS 7 web.config file and disable tracing.

<trace enabled="false" />
Saturday, February 28, 2009 5:59:57 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [2] - Trackback
dasBlog | WindowsAzure
# Tuesday, February 24, 2009

In November last year Kim Cameron wrote a series of posts about Project Geneva. It’s a little late, but still very interesting.

  1. Project Geneva - Part 1
  2. Project Geneva - Part 2
  3. Project Geneva - Part 3
  4. Project Geneva - Part 4
  5. Project Geneva - Part 5

And the PDF with the Identity Software + Services Roadmap.

Tuesday, February 24, 2009 11:09:28 AM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Security | Services
Ads
About
© Copyright 2009
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.9074.18820
All Content © 2009, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)