Deserialization from XML fails with Generic Parameters

Nov 19, 2008 at 8:34 AM
Hello

suppose that we have the following Expression

Expression <Func<Foo,bool>>  myEx =  x = > x.Bar<string>();


Here bar is a method that takes a generic parameter, deserialization of this expression from xml fails with an XML error. Workarounds ?
Developer
Nov 20, 2008 at 7:00 PM
Hi,

There is a solution for this problem, and it will be uploaded in a couple of days. In the meantime, here's a fix you can apply in your code (just change it and recompile the project).

In the Extensions folder, replace the methods code in the ReflectionExtensions.cs file with the following:

         public static string ToSerializableForm(this MethodInfo method)
        {
            string serializableName = method.DeclaringType.AssemblyQualifiedName + Environment.NewLine;
            if (!method.IsGenericMethod)
            {
                serializableName += method.ToString();
            }
            else
            {
                serializableName += method.GetGenericMethodDefinition().ToString() + Environment.NewLine +
                    String.Join(Environment.NewLine, method.GetGenericArguments().Select(ty => ty.ToSerializableForm()).ToArray());
            }
            return serializableName;
        }

       public static MethodInfo FromSerializableForm(this MethodInfo methodInfo, string serializedValue)
       {
            string[] fullName = SplitString(serializedValue);
            string name = fullName[1];           
            MethodInfo method = (from m in Type.GetType(fullName[0]).GetMethods()
                                 where m.ToString() == name
                                 select m).First();

            if (method.IsGenericMethod)
            {
                method = method.MakeGenericMethod(fullName.Skip(2).Select(s => typeof(string).FromSerializableForm(s)).ToArray());
            }
            return method;
        }


this code will not change serialized trees that didn't use generic methods, but will create new serialization data for generic methods - so just re-run the serialization for any expressions that contained generic method

I will add this code to the current version and update the version in a couple of days.

Ido.
Nov 21, 2008 at 7:50 AM
thanks a million you are awesome!

On Thu, Nov 20, 2008 at 9:00 PM, [email removed] wrote:
> From: idof
>
> Hi,
>
> There is a solution for this problem, and it will be uploaded in a couple of
> days. In the meantime, here's a fix you can apply in your code (just change
> it and recompile the project).
>
> In the Extensions folder, replace the methods code in the
> ReflectionExtensions.cs file with the following:
>
> public static string ToSerializableForm(this MethodInfo method)
> {
> string serializableName =
> method.DeclaringType.AssemblyQualifiedName + Environment.NewLine;
> if (!method.IsGenericMethod)
> {
> serializableName += method.ToString();
> }
> else
> {
> serializableName +=
> method.GetGenericMethodDefinition().ToString() + Environment.NewLine +
> String.Join(Environment.NewLine,
> method.GetGenericArguments().Select(ty =>
> ty.ToSerializableForm()).ToArray());
> }
> return serializableName;
> }
>
> public static MethodInfo FromSerializableForm(this MethodInfo
> methodInfo, string serializedValue)
> {
> string[] fullName = SplitString(serializedValue);
> string name = fullName[1];
> MethodInfo method = (from m in
> Type.GetType(fullName[0]).GetMethods()
> where m.ToString() == name
> select m).First();
>
> if (method.IsGenericMethod)
> {
> method = method.MakeGenericMethod(fullName.Skip(2).Select(s
> => typeof(string).FromSerializableForm(s)).ToArray());
> }
> return method;
> }
>
> this code will not change serialized trees that didn't use generic methods,
> but will create new serialization data for generic methods - so just re-run
> the serialization for any expressions that contained generic method
>
> I will add this code to the current version and update the version in a
> couple of days.
>
> Ido.
>
> Read the full discussion online.
>
> To add a post to this discussion, reply to this email
> ([email removed])
>
> To start a new discussion for this project, email
> [email removed]
>
> You are receiving this email because you subscribed to this discussion on
> CodePlex. You can unsubscribe on codePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any posts
> to this discussion will also be available online at codeplex.com