20110511

even better type conversion between similar objects

My last post showed how to use compiled linq expressions as delegates to make a type converter.  I noted that an MSIL solution would be faster.  I am not a MSIL expert by any means, but here is my best attempt as using DynamicMethod and MSIL to create a delegate converter.

It does the same thing as the linq version but requires separate assign blocks for each combination of field and property.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;

namespace typeoverridedemostrater
{

    static class SuperConverter<T1, T2> where T2 : new()
    {

        private static Dictionary<Tuple<System.Type, System.Type>, MethodInvoker> converters = new Dictionary<Tuple<System.Type, System.Type>, MethodInvoker>();
        delegate T2 MethodInvoker(T1 inpt);

        static public T2 Convert(T1 i)
        {
            var key = new Tuple<System.Type, System.Type>(typeof(T2), typeof(T1));
            if (!converters.ContainsKey(key))
            {
                DynamicMethod dm = new DynamicMethod("xmethod", typeof(T2), new Type[] { typeof(T1) }, typeof(T2));
                ILGenerator il = dm.GetILGenerator();
                il.DeclareLocal(typeof(T2));
                il.Emit(OpCodes.Newobj, typeof(T2).GetConstructor(System.Type.EmptyTypes));
                il.Emit(OpCodes.Stloc_0);

                (from z in typeof(T1).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 join y in typeof(T2).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_0);
                     il.Emit(OpCodes.Ldfld, typeof(T1).GetField(a));
                     il.Emit(OpCodes.Stfld, typeof(T2).GetField(a));
                 });

                (from z in typeof(T1).GetProperties().Select(a => new { name = a.Name, type = a.PropertyType })
                 join y in typeof(T2).GetProperties().Where(b => b.CanWrite).Select(a => new { name = a.Name, type = a.PropertyType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_0);
                     il.Emit(OpCodes.Callvirt, typeof(T1).GetProperty(a).GetGetMethod());
                     il.Emit(OpCodes.Callvirt, typeof(T2).GetProperty(a).GetSetMethod());
                 });


                (from z in typeof(T1).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 join y in typeof(T2).GetProperties().Where(b => b.CanWrite).Select(a => new { name = a.Name, type = a.PropertyType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_0);
                     il.Emit(OpCodes.Ldfld, typeof(T1).GetField(a));
                     il.Emit(OpCodes.Callvirt, typeof(T2).GetProperty(a).GetSetMethod());
                 });

                (from z in typeof(T1).GetProperties().Select(a => new { name = a.Name, type = a.PropertyType })
                 join y in typeof(T2).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_0);
                     il.Emit(OpCodes.Callvirt, typeof(T1).GetProperty(a).GetGetMethod());
                     il.Emit(OpCodes.Stfld, typeof(T2).GetField(a));
                 });

                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Ret);

                var dg = (MethodInvoker)dm.CreateDelegate(typeof(MethodInvoker));

                converters.Add(key, dg);
            }

            return (T2)converters[key](i);
        }
    }
}

Compared to the Linq version at 100 million repetitions (at lower numbers the results are less predictable):

Linq: 94.579 seconds vs MSIL: 93.423 seconds


Some of that time is consumed dealing with the delegates, so you can transform that into a full dynamic assembly where you can have base classes and real instance references like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Threading;
using System.Reflection;


namespace typeoverridedemostrater
{

    static class SuperConverterII<T1, T2> where T2 : new()
    {

        private static Dictionary<Tuple<System.Type, System.Type>, convertbase<T1, T2>> converters = new Dictionary<Tuple<System.Type, System.Type>, convertbase<T1, T2>>();

        static public T2 Convert(T1 i)
        {
            var key = new Tuple<System.Type, System.Type>(typeof(T2), typeof(T1));
            if (!converters.ContainsKey(key))
            {

                var xassemblybuilder = Thread.GetDomain().DefineDynamicAssembly(new AssemblyName("xassembly"), System.Reflection.Emit.AssemblyBuilderAccess.Run);
                var xtype = xassemblybuilder.DefineDynamicModule("xmodule").DefineType("xtype", TypeAttributes.Public, typeof(convertbase<T1, T2>));
                var xfunction = xtype.DefineMethod("convert", MethodAttributes.Public | MethodAttributes.Virtual, typeof(T2), new System.Type[] { typeof(T1) });
                xtype.DefineMethodOverride(xfunction, typeof(convertbase<T1, T2>).GetMethod("convert"));

                var il = xfunction.GetILGenerator();

                il.DeclareLocal(typeof(T2));

                il.Emit(OpCodes.Newobj, typeof(T2).GetConstructor(System.Type.EmptyTypes));
                il.Emit(OpCodes.Stloc_0);

                (from z in typeof(T1).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 join y in typeof(T2).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_1);
                     il.Emit(OpCodes.Ldfld, typeof(T1).GetField(a));
                     il.Emit(OpCodes.Stfld, typeof(T2).GetField(a));
                 });

                (from z in typeof(T1).GetProperties().Select(a => new { name = a.Name, type = a.PropertyType })
                 join y in typeof(T2).GetProperties().Where(b => b.CanWrite).Select(a => new { name = a.Name, type = a.PropertyType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_1);
                     il.Emit(OpCodes.Callvirt, typeof(T1).GetProperty(a).GetGetMethod());
                     il.Emit(OpCodes.Callvirt, typeof(T2).GetProperty(a).GetSetMethod());
                 });


                (from z in typeof(T1).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 join y in typeof(T2).GetProperties().Where(b => b.CanWrite).Select(a => new { name = a.Name, type = a.PropertyType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_1);
                     il.Emit(OpCodes.Ldfld, typeof(T1).GetField(a));
                     il.Emit(OpCodes.Callvirt, typeof(T2).GetProperty(a).GetSetMethod());
                 });

                (from z in typeof(T1).GetProperties().Select(a => new { name = a.Name, type = a.PropertyType })
                 join y in typeof(T2).GetFields().Select(a => new { name = a.Name, type = a.FieldType })
                 on z equals y
                 select z.name).ToList().ForEach(a =>
                 {
                     il.Emit(OpCodes.Ldloc_0);
                     il.Emit(OpCodes.Ldarg_1);
                     il.Emit(OpCodes.Callvirt, typeof(T1).GetProperty(a).GetGetMethod());
                     il.Emit(OpCodes.Stfld, typeof(T2).GetField(a));
                 });

                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Ret);

                xtype.CreateType();

                converters.Add(key, (convertbase<T1, T2>)xassemblybuilder.CreateInstance("xtype"));
            }
            return converters[key].convert(i);
        }

    }

    public abstract class convertbase<T1, T2>
    {
        public abstract T2 convert(T1 i);
    }
}

Compared to the original MSIL version at 100 million repetitions:

Delegate MSIL: 93.423 seconds vs Base Class MSIL: 91.814 seconds

At REALLY high volumes that is significant, and I am certain that someone better at MSIL than I could probably make it a bit faster still, but for my purposes the Linq version is more than sufficient as I find the MSIL versions a little hard to work in for the level of benefit in return.

20110509

better type conversion between similar objects

Friday I posted what is about the simplest possible way to automagicly convert between two objects that have mostly the same fields and properties but have no implicit conversion or common interface or base type.  It uses reflection and a little Linq, but it uses reflection every single time so it does not perform very well under load.  You can see that post here.

I wrote that as a quick hack to solve a specific, temporary problem a few weeks ago and I knew it wasn't going to be very efficient, but it didn't matter in that instance.  The best solutions in this sort of problem are MSIL assuming you cannot just change the classes to share dependencies, but MSIL is not something very many folks can read, and even fewer can write.

In .Net 4, you get some interesting additions/better documentation to Linq show you how to build expressions on the fly, so I wanted to take a stab at re-writing the converter that way.

The general idea is the same, use reflection to look for fields and writable properties then generate some action to sync them up, but there are several differences:
  • reflection is only used the first time a pair of types is converted
  • the actions are generated as Linq that is then compiled before storing in the converters set
  • added the ability to set a property to a field or field to property
  • added type checking
This is not going to perform as well as MSIL, but for a team with little MSIL experience it is going to be far more supportable and it seems to perform reasonably well.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace typeoverridedemostrater
{
    static class SuperConverter<T1, T2> where T2 : new()
    {

        private static Dictionary<Tuple<System.Type, System.Type>, Func<T1, T2>> converters = new Dictionary<Tuple<System.Type, System.Type>, Func<T1, T2>>();

        static public T2 Convert(T1 i)
        {
            var key = new Tuple<System.Type, System.Type>(typeof(T2), typeof(T1));
            if (!converters.ContainsKey(key))
            {

                ParameterExpression value = Expression.Parameter(typeof(T1), "value");
                ParameterExpression result = Expression.Parameter(typeof(T2), "result");
                var exprs = new List<Expression>();
                exprs.Add(Expression.Assign(result, Expression.New(typeof(T2))));

                exprs.AddRange((
                        from z in typeof(T2).GetProperties().Where(a => a.CanWrite)
                            .Select(b => new { name = b.Name, type = b.PropertyType }).Union(
                            typeof(T2).GetFields().Select(c => new { name = c.Name, type = c.FieldType }))
                        join y in typeof(T1).GetProperties().Where(a => a.CanWrite)
                            .Select(b => new { name = b.Name, type = b.PropertyType }).Union(
                            typeof(T1).GetFields().Select(c => new { name = c.Name, type = c.FieldType }))
                        on z equals y
                        select
                            Expression.Assign(Expression.PropertyOrField(result, z.name), Expression.PropertyOrField(value, z.name))
                        ).ToArray()
                    );
                exprs.Add(result);
                BlockExpression block = Expression.Block(variables: new[] { result }, expressions: exprs.ToArray());
                converters.Add(key, Expression.Lambda<Func<T1, T2>>(block, value).Compile());
            }
            return converters[key].Invoke(i);
        }
    }
}


the general flow is:
  1. if you don't already have a good converter
  2. create an input value and output result
  3. create an action to assign a new instance to the result variable
  4. add an assign action for every param/field that has a match between the types
  5. add an action for returning the result
  6. turn those into an expression block
  7. turn the block Lambda, then compiled to a TDelegate and store in the converters dictonary
  8. run the TDelegate from the dictionary


For 100,000 conversions the before/after is:

TotalMilliseconds = 1609.4059 vs TotalMilliseconds = 93.7518

20110506

Cheap type conversion for very similar objects

Ever get stuck between two API and have to keep converting between two almost identical sets of data objects because neither API can be modded at the moment?  If you can afford some performance penalty, this is a quick way to get fields and properties with the same names synced.

First you add some implicit type operators:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace typeoverridedemostrater
{
    class Class2
    {
        public string string1;
        public string string2;
        public string string3;
 
        public static implicit operator Class2(Class1 initialData)
        {
            return new SuperConverter<Class1Class2>().Convert(initialData);
        }
 
        public static implicit operator Class2(Class3 initialData)
        {
            return new SuperConverter<Class3Class2>().Convert(initialData);
        }
    }
}

After  you call the converter, you can add whatever specific conversions you need.

(EDIT - as Brooke pointed out, the real version of this was an extension to an existing class, not a class with an internal operator the code above was from the demo project I bashed out to explain the concept to another dev)

Then you use some reflection and linq to do the assignments in a separate class that you can re-use in as many type converters as you like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace typeoverridedemostrater
{
    class SuperConverter where T2 : new()
    {
        public T2 Convert(T1 i)
        {
            var temp = new T2();
 
            typeof(T1).GetProperties().ToList().Where(a => a.CanWrite).ToList().
                ForEach(b =>
                {
                    var z = temp.GetType().GetProperty(b.Name);
                    if (z != null) z.SetValue(temp, b.GetValue(i, null), null);
                });
            
            typeof(T1).GetFields().ToList().ToList().
                ForEach(b =>
                {
                    var z = temp.GetType().GetField(b.Name);
                    if (z != null) z.SetValue(temp, b.GetValue(i));
                });
 
            return temp;
        }
    }
}


Obviously this only works in some cases, and if your types do not match precisely you would have to make it a little more complex by checking for compatible types, or handling failure more gracefully, but it can save a ton of time in some cases.

20110504

Adding a WCF function to a service, in code, at runtime

Working on something last week I thought it might be a good idea to have an attribute that added an extra endpoint to a service at runtime.  I coded for a bit making decent progress, got stuck, and when I started to google I saw several discussions of it not being possible, and one discussion about how to do it by modifying the host (http://zamd.net/2010/02/05/adding-dynamic-methods-to-a-wcf-service/).  It turns out that after the service is running some things seem to be locked down that you would need to pull this trick off.

Remembering some similar issues in HttpModules/HttpApplications I started looking for a point to do the injection of a method while the service is in the middle of loading and I found ServiceBehavior.AddBindingParameters.  The other two methods don't seem to be at the right point in the sequence for the injection to work correctly.

WARNING: lots of trial & error based code ahead, I doubt this is correct in all situations, it does work on a REST POX service fairly well.  I am mostly writing this so I remember it, and so others can see it probably can be done.

First you have to make an attribute that you will use later to decorate your service implementation.  In this example the AddUI sub looks at the first operation for the current contract, copies a lot of it's general settings then adds the messages to an operation, then the operation to the contract.

Imports System.ServiceModel.Description
 
Public Class ExternalInvalidatorAttribute
    Inherits Attribute
    Implements IServiceBehavior
 
    Public Sub AddBindingParameters(ByVal serviceDescription As System.ServiceModel.Description.ServiceDescriptionByVal serviceHostBase As System.ServiceModel.ServiceHostBaseByVal endpoints As System.Collections.ObjectModel.Collection(Of System.ServiceModel.Description.ServiceEndpoint), ByVal bindingParameters As System.ServiceModel.Channels.BindingParameterCollectionImplements System.ServiceModel.Description.IServiceBehavior.AddBindingParameters
        For Each endpt In endpoints
            AddUI(endpt)
        Next
    End Sub
 
    Private Sub AddUI(ByRef endpt As ServiceEndpoint)
        Dim cd = endpt.Contract.Operations(0).DeclaringContract
        Dim od = New OperationDescription("invalidatorUI", cd)
 
        Dim inputMsg = New MessageDescription(cd.Namespace + cd.Name + "/invalidatorUI"MessageDirection.Input)
        Dim mpd = New MessagePartDescription("a", endpt.Contract.Namespace)
        mpd.Index = 0
        mpd.MemberInfo = Nothing
        mpd.Multiple = False
        mpd.ProtectionLevel = Net.Security.ProtectionLevel.None
        mpd.Type = GetType(System.String)
        inputMsg.Body.Parts.Add(mpd)
        od.Messages.Add(inputMsg)
 
        Dim outputMsg = New MessageDescription(cd.Namespace + cd.Name + "/invalidatorUIResponse"MessageDirection.Output)
        outputMsg.Body.ReturnValue = New MessagePartDescription("invalidatorUIResult", cd.Namespace) With {.Type = GetType(System.String)}
        od.Messages.Add(outputMsg)
 
        od.Behaviors.Add(New DataContractSerializerOperationBehavior(od))
        od.Behaviors.Add(New System.ServiceModel.Web.WebGetAttribute() With {.UriTemplate = "/invalidator/{a}"})
        od.Behaviors.Add(New System.ServiceModel.OperationBehaviorAttribute())
        Dim dc = New DasOP()
 
        od.Behaviors.Add(dc)
 
        cd.Operations.Add(od)
    End Sub
 
 
#Region "not needed"
 
    Public Sub ApplyDispatchBehavior(ByVal serviceDescription As System.ServiceModel.Description.ServiceDescriptionByVal serviceHostBase As System.ServiceModel.ServiceHostBaseImplements System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior
    End Sub
 
    Public Sub Validate(ByVal serviceDescription As System.ServiceModel.Description.ServiceDescriptionByVal serviceHostBase As System.ServiceModel.ServiceHostBaseImplements System.ServiceModel.Description.IServiceBehavior.Validate
    End Sub
 
#End Region
 
End Class

Since there is no function in the service implementation the default invoker call would error with nothing to act upon, so the DasOP custom operation behavior is substituted for the default.  Looking at it's apply dispatch sub below you can see it is just a crutch to a custom invoker.  You don't have to do this, but if you don't you have to deal with the message objects by hand, and they are not really that friendly. 

Imports System.ServiceModel.Description
 
Public Class DasOp
    Inherits Attribute                                  'this makes it a decorator
    Implements IOperationBehavior                       'this makes it get called for applybehaviour
 
    Public Sub ApplyDispatchBehavior(ByVal operationDescription As System.ServiceModel.Description.OperationDescriptionByVal dispatchOperation As System.ServiceModel.Dispatcher.DispatchOperationImplements System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior
        dispatchOperation.Invoker = New InvalidatorInvoker()    'this invoker actually does the work, it needs a reference to the other cache objects so it can meddle in their cache arrays
    End Sub
 
#Region "not used"
    Public Sub AddBindingParameters(ByVal operationDescription As System.ServiceModel.Description.OperationDescriptionByVal bindingParameters As System.ServiceModel.Channels.BindingParameterCollectionImplements System.ServiceModel.Description.IOperationBehavior.AddBindingParameters
        'not needed
    End Sub
 
    Public Sub ApplyClientBehavior(ByVal operationDescription As System.ServiceModel.Description.OperationDescriptionByVal clientOperation As System.ServiceModel.Dispatcher.ClientOperationImplements System.ServiceModel.Description.IOperationBehavior.ApplyClientBehavior
        'not needed
    End Sub
 
    Public Sub Validate(ByVal operationDescription As System.ServiceModel.Description.OperationDescriptionImplements System.ServiceModel.Description.IOperationBehavior.Validate
        'not needed
    End Sub
#End Region
 
End Class

Finally you make the invoker, it doesn't really invoke anything, since nothing actually exists to invoke, but it allocates and input and returns a result like their was, so the rest of the WCF seems not to notice the difference.

Imports System.ServiceModel.Dispatcher
Imports System.Xml.Linq
Imports System.Text.RegularExpressions
 
Public Class InvalidatorInvoker
    Implements IOperationInvoker
 
    Public Function AllocateInputs() As Object() Implements System.ServiceModel.Dispatcher.IOperationInvoker.AllocateInputs
        Return {Nothing}    'reserve a spot for some input
    End Function
 

   Public Function Invoke(ByVal instance As ObjectByVal inputs() As ObjectByRef outputs() As ObjectAs Object Implements System.ServiceModel.Dispatcher.IOperationInvoker.Invoke
        outputs = New Object(-1) {}     'return an empty array here, MSDN does not elaborate as to why
        Return "Result"
   End Function
 
#Region "not needed"
 
    Public Function InvokeBegin(ByVal instance As ObjectByVal inputs() As ObjectByVal callback As System.AsyncCallbackByVal state As ObjectAs System.IAsyncResult Implements System.ServiceModel.Dispatcher.IOperationInvoker.InvokeBegin
        Return Nothing
    End Function
 
    Public Function InvokeEnd(ByVal instance As ObjectByRef outputs() As ObjectByVal result As System.IAsyncResultAs Object Implements System.ServiceModel.Dispatcher.IOperationInvoker.InvokeEnd
        Return Nothing
    End Function
 
#End Region
 
    Public ReadOnly Property IsSynchronous As Boolean Implements System.ServiceModel.Dispatcher.IOperationInvoker.IsSynchronous
        Get
            Return True     'disable async
        End Get
    End Property
 
End Class

It isn't pretty, but it works.

Firefox Feedly RSS option

If you use Firefox with a RSS button and want the default RSS page to offer a Feedly option here is what you need to do: go to the about:c...