Click here to Skip to main content
12,999,833 members (52,681 online)
Click here to Skip to main content
Articles » Languages » C# » Reflection » Downloads


147 bookmarked
Posted 18 Apr 2007

HyperDescriptor: Accelerated dynamic property access

, 20 Apr 2007
Provides a vastly accelerate runtime property implementation that can be applied even to closed-source classes
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Security.Permissions;

/* Change history:
 * 20 Apr 2007  Marc Gravell    Rollback dictionary on error;
 *                              Assert ReflectionPermission for main creation
 *                                  (thanks/credit to Josh Smith for feedback/hints)

namespace Hyper.ComponentModel {
    public sealed class HyperTypeDescriptionProvider : TypeDescriptionProvider {
        public static void Add(Type type) {
            TypeDescriptionProvider parent = TypeDescriptor.GetProvider(type);
            TypeDescriptor.AddProvider(new HyperTypeDescriptionProvider(parent), type);
        public HyperTypeDescriptionProvider() : this(typeof(object)) { }
        public HyperTypeDescriptionProvider(Type type) : this(TypeDescriptor.GetProvider(type)) { }
        public HyperTypeDescriptionProvider(TypeDescriptionProvider parent) : base(parent) { }
        public static void Clear(Type type) {
            lock (descriptors) {
        public static void Clear() {
            lock (descriptors) {
        private static readonly Dictionary<Type, ICustomTypeDescriptor> descriptors = new Dictionary<Type, ICustomTypeDescriptor>();
        public sealed override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) {
            ICustomTypeDescriptor descriptor;
            lock (descriptors) {
                if (!descriptors.TryGetValue(objectType, out descriptor)) {
                        descriptor = BuildDescriptor(objectType);
                        return base.GetTypeDescriptor(objectType, instance);
                return descriptor;
        [ReflectionPermission( SecurityAction.Assert, Flags = ReflectionPermissionFlag.AllFlags)]
        private ICustomTypeDescriptor BuildDescriptor(Type objectType)
            // NOTE: "descriptors" already locked here

            // get the parent descriptor and add to the dictionary so that
            // building the new descriptor will use the base rather than recursing
            ICustomTypeDescriptor descriptor = base.GetTypeDescriptor(objectType, null);
            descriptors.Add(objectType, descriptor);
                // build a new descriptor from this, and replace the lookup
                descriptor = new HyperTypeDescriptor(descriptor);
                descriptors[objectType] = descriptor;
                return descriptor;
            {   // rollback and throw
                // (perhaps because the specific caller lacked permissions;
                // another caller may be successful)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Marc Gravell
Software Developer Stack Exchange
United Kingdom United Kingdom
Geek at Stack Exchange

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Marc Gravell
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid