Category Archives: 12314

Extension Methods And Type Inference In Action

I make extensive use of extension methods, either to make classes small and focused or to improve readability.

While porting a .NET 1.1 WinForms application to C# 3.0, I found lots of code like this:

delegate int Int32DelegateStringBoolean(string text, bool flag);

void DoStuff()
{
    // ...

    var x = (int)this.Invoke(new Int32DelegateStringBoolean(GetStuff), new object[] { "some text", true });

    // ...
}

int GetStuff(string text, bool flag)
{
    // ...
}


.NET 2.0 introduced a nicer API and it became possible to write the code calling Invoke like this:



var x = (int)this.Invoke(new Int32DelegateStringBoolean(GetStuff), "some text", true);


But it’s still not strongly typed enough to my taste and the compiler can’t verify if any mistake was made. This will still be valid code at compile time that will break at run time:



var x = (long)this.Invoke(new Int32DelegateStringBoolean(GetStuff), "some text", 10M, 5);


To make the code safer and more readable, I decided to create extension methods to extend the Control class and provide strongly type Invoke methods.



Instead of defining custom delegates for each need, I used the Action and Func delegates exiting in the framework and wrote extension methods like this:



public static TResult InvokeFunc<T1, T2, TResult>(this Control control, Func<T1, T2, TResult> func, T1 param1, T2 param2)
{
    return (TResult)(control.Invoke(func, param1, param2));
}


Now I can replace the call to Invoke with this call:



var x = this.InvokeFunc<string, bool, int>(new Func<string, bool, int>(GetStuff), "some text", true);


And the compiler is now able to match the type of the delegate, the parameters and the return value.



Starting with the C# 2.0 compiler, there is no need to write the delegate instantiation if the compiler can infer the type of the delegate, which makes the code even simpler:



var x = this.InvokeFunc<string, bool, int>(GetStuff, "some text", true);


The C# compiler is even capable of inferring the type parameters of the InvokeFunc method making the code even smaller and more readable:



var x = this.InvokeFunc(GetStuff, "some text", true);


A lot better than what we started with, isn’t it?



So far, I’ve implemented these:



/// <summary>
/// Provides extended functionality to <see cref="System.Windows.Forms.Control"/>.
/// </summary>
public static class ControlExtensions
{
    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<TResult>(this Control control, Func<TResult> func)
    {
        return (TResult)(control.Invoke(func));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The parameter of the action.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, TResult>(this Control control, Func<T1, TResult> func, T1 param1)
    {
        return (TResult)(control.Invoke(func, param1));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, T2, TResult>(this Control control, Func<T1, T2, TResult> func, T1 param1, T2 param2)
    {
        return (TResult)(control.Invoke(func, param1, param2));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, T2, T3, TResult>(this Control control, Func<T1, T2, T3, TResult> func, T1 param1, T2 param2, T3 param3)
    {
        return (TResult)(control.Invoke(func, param1, param2, param3));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <param name="param4">The forth parameter of the function.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, T2, T3, T4, TResult>(this Control control, Func<T1, T2, T3, T4, TResult> func, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        return (TResult)(control.Invoke(func, param1, param2, param3, param4));
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    public static void InvokeAction(this Control control, Action action)
    {
        control.Invoke(action);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T">The type of the parameter.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param">The parameter of the action.</param>
    public static void InvokeAction<T>(this Control control, Action<T> action, T param)
    {
        control.Invoke(action, param);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    public static void InvokeAction<T1, T2>(this Control control, Action<T1, T2> action, T1 param1, T2 param2)
    {
        control.Invoke(action, param1, param2);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    public static void InvokeAction<T1, T2, T3>(this Control control, Action<T1, T2, T3> action, T1 param1, T2 param2, T3 param3)
    {
        control.Invoke(action, param1, param2, param3);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the action.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    /// <param name="param4">The forth parameter of the action.</param>
    public static void InvokeAction<T1, T2, T3, T4>(this Control control, Action<T1, T2, T3, T4> action, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        control.Invoke(action, param1, param2, param3, param4);
    }
    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<TResult>(this Control control, Func<TResult> func)
    {
        return control.BeginInvoke(func);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, TResult>(this Control control, Func<T1, TResult> func, T1 param1)
    {
        return control.BeginInvoke(func, param1);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, T2, TResult>(this Control control, Func<T1, T2, TResult> func, T1 param1, T2 param2)
    {
        return control.BeginInvoke(func, param1, param2);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, T2, T3, TResult>(this Control control, Func<T1, T2, T3, TResult> func, T1 param1, T2 param2, T3 param3)
    {
        return control.BeginInvoke(func, param1, param2, param3);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <param name="param4">The forth parameter of the function.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, T2, T3, T4, TResult>(this Control control, Func<T1, T2, T3, T4, TResult> func, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        return control.BeginInvoke(func, param1, param2, param3, param4);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction(this Control control, Action action)
    {
        return control.BeginInvoke(action);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T">The type of the parameter.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param">The parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T>(this Control control, Action<T> action, T param)
    {
        return control.BeginInvoke(action, param);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T1, T2>(this Control control, Action<T1, T2> action, T1 param1, T2 param2)
    {
        return control.BeginInvoke(action, param1, param2);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T1, T2, T3>(this Control control, Action<T1, T2, T3> action, T1 param1, T2 param2, T3 param3)
    {
        return control.BeginInvoke(action, param1, param2, param3);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the action.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    /// <param name="param4">The forth parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T1, T2, T3, T4>(this Control control, Action<T1, T2, T3, T4> action, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        return control.BeginInvoke(action, param1, param2, param3, param4);
    }
}


Use them if you need to.

Hydrating Objects With Expression Trees – Part III

LINQ With C# (Portuguese)

To finalize this series on object hydration, I’ll show some performance comparisons between the different methods of hydrating objects.

For the purpose of this exercise, I’ll use this class:

class SomeType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTimeOffset CreationTime { get; set; }
    public Guid UniqueId { get; set; }
}


and this set of data:



var data = (
    from i in Enumerable.Range(1, ObjectCount)
    select new object[] { i, i.ToString(), DateTimeOffset.Now, Guid.NewGuid() }
).ToArray();


The data bellow shows the time (in seconds) for different runs for different values of ObjectCount (in the same machine with approximately the same load) as well as it’s availability for different version of the .NET Framework and the C# programming language:



10000 100000 1000000 Valid for
Setup Hydrate Total Setup Hydrate Total Setup Hydrate Total Framework version C# Version
Activation and Reflection setter 0.060 0.101 0.161 0.055 0.736 0.791 0.054 6.822 6.876 1.0, 1.1, 2.0, 3.5, 4.0 1.0, 2.0, 3.0, 4.0
Activation and Expression Tree setter 0.300 0.003 0.303 0.313 0.049 0.359 0.293 0.578 0.871 4.0 none
Member Initializer 0.035 0.001 0.036 0.039 0.027 0.066 0.041 0.518 0.559 3.5, 4.0 3.0, 4.0


These values will vary with the number of the objects being hydrated and the number of its properties, but the method using the Member Initializer will be the most performant.



Code samples for this series of posts (and the one about object dumping with expression trees) can be found on my MSDN Code Gallery: Dump And Hydrate Objects With Expression Trees

Hydrating Objects With Expression Trees – Part II

LINQ With C# (Portuguese)

In my previous post I showed how to hydrate objects by creating instances and setting properties in those instances.

But, if the intent is to hydrate the objects from data, why not having an expression that does just that? That’s what the member initialization expression is for.

To create such an expression we need the constructor expression and the property binding expressions:

var properties = objectType.GetProperties();
var bindings = new MemberBinding[properties.Length];
var valuesArrayExpression = Expression.Parameter(typeof(object[]), "v");

for (int p = 0; p < properties.Length; p++)
{
    var property = properties[p];

    bindings[p] = Expression.Bind(
        property,
        Expression.Convert(
            Expression.ArrayAccess(
                valuesArrayExpression,
                Expression.Constant(p, typeof(int))
            ),
            property.PropertyType
        )
    );
}

var memberInitExpression = Expression.MemberInit(
    Expression.New(objectType),
    bindings
);

var objectHidrationExpression = Expression.Lambda<Func<object[], object>>(memberInitExpression, valuesArrayExpression);

var compiledObjectHidrationExpression = objectHidrationExpression.Compile();


This might seem more complex than the previous solution, but using it is a lot more simple:



for (int o = 0; o < objects.Length; o++)
{
    newObjects[o] = compiledObjectHidrationExpression(objects[o]);
}

Mastering Expression Trees With .NET Reflector

Following my last post, I received lots of enquiries about how got to master the creation of expression trees.

The answer is: .NET Reflector

On that post I needed to to generate an expression tree for this expression:

Expression<Func<object, object>> expression = o => ((object)((SomeType)o).Property1);


I just compiled that code in Visual Studio 2010, loaded the assembly in .NET Reflector, and disassembled it to C# without optimizations (View –> Options –> Disassembler –> Optimization: None).



The disassembled code looked like this:



Expression<Func<object, object>> expression;
ParameterExpression CS$0$0000;
ParameterExpression[] CS$0$0001;
expression = Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.Property(Expression.Convert(CS$0$0000 = Expression.Parameter(typeof(object), "o"), typeof(SomeType)), (MethodInfo) methodof(SomeType.get_Property1)), typeof(object)), new ParameterExpression[] { CS$0$0000 });


After giving valid C# names to the variables and tidy up the code a bit, I came up with this:



ParameterExpression parameter = Expression.Parameter(typeof(object), "o");
Expression<Func<object, object>> expression =
    Expression.Lambda<Func<object, object>>(
        Expression.Convert(
            Expression.Property(
                Expression.Convert(
                    parameter,
                    typeof(SomeType)
                ),
                "Property1"
            ),
            typeof(object)
        ),
        parameter
    );


Easy! Isn’t it?

Dumping Objects Using Expression Trees

LINQ With C# (Portuguese)


No. I’m not proposing to get rid of objects.


A colleague of mine was asked if I knew a way to dump a list of objects of unknown type into a DataTable with better performance than the way he was using.


The objects being dumped usually have over a dozen of properties, but, for the sake of this post, let’s assume they look like this:


class SomeClass
{
    public int Property1 { get; set; }
    public long Property2 { get; set; }
    public string Property3 { get; set; }
    public object Property4 { get; set; }
    public DateTimeOffset Property5 { get; set; }
}

The code he was using was something like this:


var properties = objectType.GetProperties();

foreach (object obj in objects)
{
    foreach (var property in properties)
    {
        property.GetValue(obj, null);
    }
}

For a list of one million objects, this is takes a little over 6000 milliseconds on my machine.


I immediately thought: Expression Trees!


If the type of the objects was know at compile time, it would be something like this:


Expression<Func<SomeClass, int>> expression = o => o.Property1;
var compiled = expression.Compile();
var propertyValue = compiled.Invoke(obj);

But, at compile time, the type of the object and, consequently, the type of its properties, is unknown. So, we’ll need, for each property, an expression tree like this:


Expression<Func<object, object>> expression = o => ((SomeClass)o).Property1;

The previous expression gets the value of a property of the conversion of the parameter of type object to the type of the object. The result must also be converted to type object because the type of the result must match the type of the return value of the expression.


For the same type of objects, the collection of property accessors would be built this way:


var compiledExpressions = (from property in properties
                           let objectParameter = Expression.Parameter(typeof(object), "o")
                           select
                             Expression.Lambda<Func<object, object>>(
                                 Expression.Convert(
                                     Expression.Property(
                                         Expression.Convert(
                                             objectParameter,
                                             objectType
                                         ),
                                         property
                                     ),
                                     typeof(object)
                                 ),
                                 objectParameter
                             ).Compile()).ToArray();

Looks bit overcomplicated, but reading all properties of all objects for the same object set with this code:


foreach (object obj in objects)
{
    foreach (var compiledExpression in compiledExpressions)
    {
        compiledExpression (obj);
    }
}

takes a little over 150 milliseconds on my machine.


That’s right. 2.5% of the previous time.

LINQ: Enhancing Distinct With The SelectorEqualityComparer

LINQ With C# (Portuguese)

On my last post, I introduced the PredicateEqualityComparer and a Distinct extension method that receives a predicate to internally create a PredicateEqualityComparer to filter elements.

Using the predicate, greatly improves readability, conciseness and expressiveness of the queries, but it can be even better. Most of the times, we don’t want to provide a comparison method but just to extract the comaprison key for the elements.

So, I developed a SelectorEqualityComparer that takes a method that extracts the key value for each element. Something like this:

public class SelectorEqualityComparer<TSource, Tkey> : EqualityComparer<TSource>
    where Tkey : IEquatable<Tkey>
{
    private Func<TSource, Tkey> selector;

    public SelectorEqualityComparer(Func<TSource, Tkey> selector)
        : base()
    {
        this.selector = selector;
    }

    public override bool Equals(TSource x, TSource y)
    {
        Tkey xKey = this.GetKey(x);
        Tkey yKey = this.GetKey(y);

        if (xKey != null)
        {
            return ((yKey != null) && xKey.Equals(yKey));
        }

        return (yKey == null);
    }

    public override int GetHashCode(TSource obj)
    {
        Tkey key = this.GetKey(obj);

        return (key == null) ? 0 : key.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        SelectorEqualityComparer<TSource, Tkey> comparer = obj as SelectorEqualityComparer<TSource, Tkey>;
        return (comparer != null);
    }

    public override int GetHashCode()
    {
        return base.GetType().Name.GetHashCode();
    }

    private Tkey GetKey(TSource obj)
    {
        return (obj == null) ? (Tkey)(object)null : this.selector(obj);
    }
}



Now I can write code like this:



.Distinct(new SelectorEqualityComparer<Source, Key>(x => x.Field))



And, for improved readability, conciseness and expressiveness and support for anonymous types the corresponding Distinct extension method:



public static IEnumerable<TSource> Distinct<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
    where TKey : IEquatable<TKey>
{
    return source.Distinct(new SelectorEqualityComparer<TSource, TKey>(selector));
}



And the query is now written like this:



.Distinct(x => x.Field)



For most usages, it’s simpler than using a predicate.

LINQ: Enhancing Distinct With The PredicateEqualityComparer

LINQ With C# (Portuguese)

Today I was writing a LINQ query and I needed to select distinct values based on a comparison criteria.

Fortunately, LINQ’s Distinct method allows an equality comparer to be supplied, but, unfortunately, sometimes, this means having to write custom equality comparer.

Because I was going to need more than one equality comparer for this set of tools I was building, I decided to build a generic equality comparer that would just take a custom predicate. Something like this:

public class PredicateEqualityComparer<T> : EqualityComparer<T>
{
    private Func<T, T, bool> predicate;

    public PredicateEqualityComparer(Func<T, T, bool> predicate)
        : base()
    {
        this.predicate = predicate;
    }

    public override bool Equals(T x, T y)
    {
        if (x != null)
        {
            return ((y != null) && this.predicate(x, y));
        }

        if (y != null)
        {
            return false;
        }

        return true;
    }

    public override int GetHashCode(T obj)
    {
        // Always return the same value to force the call to IEqualityComparer<T>.Equals
        return 0;
    }
}



Now I can write code like this:



.Distinct(new PredicateEqualityComparer<Item>((x, y) => x.Field == y.Field))



But I felt that I’d lost all conciseness and expressiveness of LINQ and it doesn’t support anonymous types. So I came up with another Distinct extension method:



public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, bool> predicate)
{
    return source.Distinct(new PredicateEqualityComparer<TSource>(predicate));
}



And the query is now written like this:



.Distinct((x, y) => x.Field == y.Field)



Looks a lot better, doesn’t it? And it works wit anonymous types.



Update: I, accidently, had published the wrong version of the IEqualityComparer<T>.Equals method,

LINQ: Single vs. SingleOrDefault

LINQ With C# (Portuguese)

Like other LINQ API methods that extract a scalar value from a sequence, Single has a companion SingleOrDefault.

The documentation of SingleOrDefault states that it returns a single, specific element of a sequence of values, or a default value if no such element is found, although, in my opinion, it should state that it returns THE single, specific element of a sequence of ONE value, or a default value if no such element is found. Nevertheless, what this method does is return the default value of the source type if the sequence is empty or, like Single, throws an exception if the sequence has more than one element.

I received several comments to my last post saying that SingleOrDefault could be used to avoid an exception.

Well, it only “solves” half of the “problem”. If the sequence has more than one element, an exception will be thrown anyway.

In the end, it all comes down to semantics and intent. If it is expected that the sequence may have none or one element, than SingleOrDefault should be used. If it’s not expect that the sequence is empty and the sequence is empty, than it’s an exceptional situation and an exception should be thrown right there. And, in that case, why not use Single instead? In my opinion, when a failure occurs, it’s best to fail fast and early than slow and late.

Other methods in the LINQ API that use the same companion pattern are: ElementAt/ElementAtOrDefault, First/FirstOrDefault and Last/LastOrDefault.

LINQ: Single vs. First

LINQ With C# (Portuguese)

I’ve witnessed and been involved in several discussions around the correctness or usefulness of the Single method in the LINQ API.

The most common argument is that you are querying for the first element on the result set and an exception will be thrown if there’s more than one element. The First method should be used instead, because it doesn’t throw if the result set has more than one item.

Although the documentation for Single states that it returns a single, specific element of a sequence of values, it actually returns THE single, specific element of a sequence of ONE value. When you use the Single method in your code you are asserting that your query will result in a scalar result instead of a result set of arbitrary length.

On the other hand, the documentation for First states that it returns the first element of a sequence of arbitrary length.

Imagine you want to catch a taxi. You go the the taxi line and catch the FIRST one, no matter how many are there.

On the other hand, if you go the the parking lot to get your car, you want the SINGLE one specific car that’s yours. If your “query” “returns” more than one car, it’s an exception. Either because it “returned” not only your car or you happen to have more than one car in that parking lot. In either case, you can only drive one car at once and you’ll need to refine your “query”.

Web Site Globalization With ASP.NET Routing

For those who don’t know, I have this web site http://PauloMorgado.NET/ that I use both as a web presence besides my blogs and a playfield.

Because I write both in English and Portuguese, I wanted the web site to have both English and Portuguese versions. This is easily accomplished by using ASP.NET Globalization and Localization.

But I wanted to do more than guessing the user’s language form her/his web browser languages. I wanted something like the MSDN and TechNet web sites have where the culture is embedded in the URL which makes it easy for the user to choose in which language she/he wants to see the web site.

With the release of the ASP.NET Routing, this is as easy as writing a custom route handler that sets the culture for the request and returns the requested page handler.

Something like this:

public class GlobalizationRouteHandler : global::System.Web.Routing.IRouteHandler
{
    System.Globalization.CultureInfo culture;
    System.Globalization.CultureInfo uiCulture;

    public GlobalizationRouteHandler(System.Globalization.CultureInfo culture)
        : this(culture, culture)
    {
    }

    public GlobalizationRouteHandler(CultureInfo culture, CultureInfo uiCulture)
    {
        if (culture == null)
        {
            throw new ArgumentNullException("cultureInfo", "cultureInfo is null.");
        }

        if (uiCulture == null)
        {
            throw new ArgumentNullException("uiCulture", "uiCulture is null.");
        }

        this.culture = culture;
        this.uiCulture = uiCulture;
    }

    private GlobalizationRouteHandler()
    {
    }

    #region IRouteHandler Members

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        Thread.CurrentThread.CurrentCulture = this.culture;
        Thread.CurrentThread.CurrentUICulture = this.uiCulture;

        string path = "~/" + (requestContext.RouteData.Values["path"] as string);

        var physicalPath = requestContext.HttpContext.Server.MapPath(path);
        if (System.IO.Directory.Exists(physicalPath))
        {
            path = VirtualPathUtility.Combine(path, "Default.aspx");
        }

        var httpHandler = BuildManager.CreateInstanceFromVirtualPath(path, typeof(IHttpHandler)) as IHttpHandler;

        return httpHandler;
    }

    #endregion
}


And now it’s only a matter of registering the handled cultures:



routes.Add("en", new Route("en/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("en-US"))));
routes.Add("pt", new Route("pt/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("pt-PT"))));