简体   繁体   中英

Error System.MissingMethodException: No parameterless constructor defined for type of 'System.Linq.Expressions.Expression`1

I want to create dynamic expressions for my web services to allow client applications to pass dynamic queries to filter the data as they require. To this end I'm trying to serialize / deserialize an Expression in C# / .NET. that the client application can pass into my web service. Unfortunately I'm getting the following error when I attempt to deserialize the expression.

System.MissingMethodException: No parameterless constructor defined for type of 'System.Linq.Expressions.Expression`1[[System.Func`2[[Common.Entities.ModuleEntityAdmins, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]

The classes I am deserializing contain parameterless constructors and are decorated with the appropriate [DataContract] and [DataMember] attributes.

[DataContract]
public class ModuleEntityAdmins
{
    [DataMember]
    public List<ModuleEntityAdmin> Modules { get; set; }

    /// <summary>
    /// Default constructor
    /// </summary>
    public ModuleEntityAdmins()
    {
        this.Modules = new List<ModuleEntityAdmin>();
    }
}

[DataContract]
public class ModuleEntityAdmin
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public bool Active { get; set; }
    [DataMember]
    public string Name { get; set; }

    /// <summary>
    /// Default constructor
    /// </summary>
    public ModuleEntityAdmin()
    {
    }
}

I create a simple Expression to return a single instance of the class.

Expression<Func<ModuleEntityAdmins, ModuleEntityAdmin>> expr1 = m => m.Modules.Find(q => q.Id == 1);

I serialize the Expression using the following function.

public string SerializeObject(object objtoserialize)
{
    return JsonConvert.SerializeObject(objtoserialize);
}

At this point everythng is fine.

I then go to deserialize the string using the following function.

public T DeserializeObject<T>(string jsonObject)
{
    T result = default(T);
    if (!string.IsNullOrEmpty(jsonObject))
    {
        //errors on the line below!!
        result = new JavaScriptSerializer().Deserialize<T>(jsonObject);
    }
    return result;
}

It is when attempting to deserialize the Expression that I get the error.

I have correctly decorated the classes involved and they both have parameterless constructors so can't understand why I am getting the error.

Any ideas?

There is exists simple labrary Remote.Linq - https://github.com/6bee/Remote.Linq

You can try example code:

            Expression<Func<ModuleEntityAdmins, ModuleEntityAdmin>> expr1 = m => m.Modules.Find(q => q.Id == 1);

            var remoteExpression = expr1.ToRemoteLinqExpression();

            var s = SerializeObject(new RequestExp { Expression = expr1 });

            RequestExp requestExp = DeserializeObject< RequestExp > (s);

            Expression<Func<ModuleEntityAdmins, ModuleEntityAdmin>> expression = requestExp.Expression.ToLinqExpression<ModuleEntityAdmins, ModuleEntityAdmin>();           

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM