简体   繁体   中英

Convert between string and enum using Convert.ChangeType

I have a class which represent a generic filter expression

public class SimpleFilter : BaseRuleExpression
{
    public string field { get; set; }

    public string op { get; set; }

    public string data { get; set; }
}

When I need to apply a filter on a IQueryable I simply convert this filter to a LambdaExpression object and apply this filter to a DbSet instance like in the following example

var filter = new SimpleFilter() {
    field = "CustomerID",
    op = "eq",
    value = "1"
};
ParameterExpression pe = Expression.Parameter( typeof( Customer ), "x" );
IQueryable<Customer> query = _ctx.Customers.AsQueryable<Customer>();
Expression<Func<Customer, bool>> lambda = (Expression<Func<Customer, bool>>)filter.ToExpression( query, pe );
var cList = _ctx.Customers.AsQueryable<Customer>().Where( lambda )

The ToExpression method is as follow:

MemberExpression memberAccess = GetMemberExpression( field, pe );
ConstantExpression filter = Expression.Constant( Convert.ChangeType( data, memberAccess.Type ) );
Operator condition = (Operator)StringEnum.Parse( typeof( Operator ), op );
LambdaExpression lambda = BuildLambdaExpression( memberAccess, filter, pe, condition, data );

The problem happens on the following line because the Convert.ChangeType method has no direct conversion between string and enum .

ConstantExpression filter = Expression.Constant( Convert.ChangeType( data, memberAccess.Type ) );

How can I solve this problem? Can I check for type enum in target and eventually convert first the string in int ?

I find that getting a converter from TypeDescriptor is a more reliable way to convert between various types. This is the strategy used by technologies like ASP.NET MVC Model Binding.

void Main()
{
    var converter = TypeDescriptor.GetConverter(typeof(Foo));
    Foo f = (Foo)converter.ConvertFromString("A");
    Console.WriteLine(f); // A
}

// Define other methods and classes here
public enum Foo {A, B, C}

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