Cheating The compiler: Generic Properties

time to read 5 min | 847 words

One of the more annoying design decision in C# is the lack of generic properties. This mean that I can't do things like:

Context

.Repository<Customer>.Get(15);

This is incredibly annoying, since the syntax for the above is just ugly with methods. Using the same example as the last several ones, here is how I implemented it:

public static class Context

{

    public static class Repository<T>

    {

        static IRepository<T> inner = Container.Resolve<IRepository<T>>();

 

        public static T Get(int id)

        {

            return inner.Get(id);

        }

        ...

    }

}

I still can't figure out how to do in for non static cases (and yes, there are reasons for that), but this is a step in the right direction.