I really did no expect that.

ReSharper is crazy

Is said to in Europe, every man has an average of omega replica three watches, and to pay attention to the instrument famous Italy the average man watch has reached eight, these watches can mix and match clothing, to cope with a variety of uk replica watches occasions need. Here we are not talking about the fashion of men in Italy wear what table, but to explore the famous British Agent 007 James Bond in the film wearing what table. Isn't James Bond fashionable? The answer is obviously negative. Rolex Rolex, Omega and Seiko, is the most popular 007 favored brand, these brands period alternating from one side reflects the development path of rolex uk brand watches, and watch the performance in the film also a good interpretation of the brand connotation and the personality. ROLEX Rolex, OMEGA OMEGA and SEIKO precision, is the most popular brand of 007

While writing up an answer for a Stack Overflow question I had a code like this:

foreach (var property in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
    if (!property.CanRead)

    var targetProperty = targetType.GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance);
    if (targetProperty != null
            && targetProperty.CanWrite
            && targetProperty.PropertyType.IsAssignableFrom(property.PropertyType))
                Expression.Property(targetVariable, targetProperty),
                    Expression.Property(sourceVariable, property), targetProperty.PropertyType)));

Nothing overly complicated, but not the simplest thing either. Then I noticed a green squiggly line under the foreach. Apparently, ReSharper can convert that code into a LINQ-expression. That's interesting, I wondered, how would the result look like? Like this:

    (from property in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
        where property.CanRead
        let targetProperty = targetType.GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance)
            targetProperty != null && targetProperty.CanWrite
            && targetProperty.PropertyType.IsAssignableFrom(property.PropertyType)
                Expression.Property(targetVariable, targetProperty),
                Expression.Convert(Expression.Property(sourceVariable, property), targetProperty.PropertyType))).

Okay, the final Cast() is unnecessary (and ReSharper itself knows that and offers you to remove it), but otherwise, great job!

In the end, I chose not to use LINQ here, but it's good to know the option is there and it's so easy.

c# resharper
Posted by: Petr Onderka
Last revised: 23 Jun, 2017 01:38 PM History


07 Nov, 2012 11:04 PM

I'm sorry, I don't have much time lately, so I'm afraid I can't help you.

15 Jan, 2013 09:14 PM

It must have been very much fun to write this R# transformation as a dev at Jetbrains.

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.