views:

25

answers:

0

In my programs I prefer using strongly typed resources instead of strings. For instance I have an image resource called: Properties.Resources.Resize.
I'm using a library that only allows an Uri of this resource to add an image to a button, like this:

LargeImage = new Uri( @"/Resources/Resize.png", UriKind.Relative);

To archieve this using strongly typed resources I created the method below which uses expression trees:

    static public Uri GetBmpResourceUri<T>(Expression<Func<T>> property,string extension)
    {
        MemberExpression expr = property.Body as MemberExpression;
        string res = (new string[] { @"/", 
                                     expr.Member.DeclaringType.Name, 
                                     @"/", 
                                     expr.Member.Name, 
                                     extension }).ToOneString();
        return new Uri(res, UriKind.Relative);
    }

I can't find the type of the bitmap in the expression tree. So for now I have to add a file extension parameter to the method. (in this case ".png", anyone knows a solution to this?)
I can now convert a resource to an Uri with this call:

  LargeImage = GetBmpResourceUri(() => Properties.Resources.Resize,".png");

Does anyone know a better way to achieve this? Or is there already a method in the .net framework that does all of this?