tags:

views:

44

answers:

3

In ImageFormat, there are a few properties such as Png, Tiff etc.

Now, given a string is it possible to retrieve the corresponding static property?

Here's the code

[Test]
public void GetPng()
{
    Assert.AreEqual(ImageFormat.Png, GetImageFormat("Png"));  //how to construct a GetImageFormat function?
}
+4  A: 
public static void Main()
{
    typeof(ImageFormat).GetProperty("GetPng", BindingFlags.Public |
                                              BindingFlags.Static);
}
Leniel Macaferi
+2  A: 
PropertyInfo pi =  typeof(ImageFormat)
    .GetProperty("Png", BindingFlags.Static | BindingFlags.Public);
Mark Seemann
+1 (Best formatting wins)
James Curran
+1  A: 
static ImageFormat GetImageFormat(string name)
{
    return (ImageFormat)typeof(ImageFormat)
        .GetProperty(name)
        .GetValue(null, null);
}
VirtualBlackFox