• How does TypeDescriptor.GetProperties(...) differ from withProperties.GetType().GetProperties()?

  • Jacob: I find it easier to work with, that's all.

  • You could use this:
    new Dictionary { { "Name", "Roy" }, { "Country", "Israel" } };

    A bit heavier on the syntax, but avoids the reflection.

  • Andrew: Nice! I hadn't considered that option.

  • Roy, you can also do this now:

    new RouteValueDictionary(new {key1="value1", key2="value2"};

    RouteValueDictionary will be populated with the values from the anonymous type. Even though it is called "RouteValueDictionary" it inherits Dictionary so is more generally useful assuming you are ok with case insensitivity.

  • > Jacob: I find it easier to work with, that's all.

    Plus it uses ICustomTypeDescriptor if you've implemented it. Which is obviously not the case here, but, you know, it's really kinda annoying when Type.GetProperties() is used where TypeDescriptor should be - like in NVelocity.

  • People who don't like reflection will hate this version:

    static public IDictionary MakeDictionary(object o)
    Type type = o.GetType();
    return type.GetProperties()
    .Select(n => n.Name)
    .ToDictionary(k => k, k => type.GetProperty(k).GetValue(o, null));

Comments have been disabled for this content.