0

Simple.OData.Client 具有类型化和动态(和基本)的语法。

我喜欢打字,但我不想建立我所有的类型。最后,我得到的结果真的只需要两种左右的类型。

但是我的查询需要更多类型来正确过滤结果。

所以我想使用动态语法。但我想将结果转换为我拥有的课程。

我可以轻松地手动执行此操作,但我想我会先看看 Simple.OData.Client 是否支持此功能,然后再为每个查询编写所有转换代码。

这是一些运行时没有错误的动态语法代码:

client.For(x.Client).Top(10).Select(x.ClientId, x.Name).FindEntriesAsync();

这是我希望工作的一个例子(选择一个新的客户端对象)

client.For(x.Client).Top(10).Select(new Client(x.ClientId, x.Name)).FindEntriesAsync();

但不支持这种投影(我收到“有一些无效参数”错误)。

使用 Simple.OData.Client 的动态语法时,是否有办法支持投影到现有类?

4

1 回答 1

0

编辑:下面的代码有效。但它的性能很糟糕。我决定放弃它并为我需要的每种类型编写手写映射器。

这就是我想出的:

dynamic results = oDataClient.For(x.Client).Select(x.ClientId, x.Name).FindEntriesAsync().Result;   
var listOfClients = SimpleODataToList<Client>(results);


public List<T> SimpleODataToList<T>(dynamic sourceObjects) where T : new()
{
    List<T> targetList = new List<T>();

    foreach (var sourceObject in sourceObjects)
    {
        // This is a dictionary with keys (properties) and values.  But no 
        //  matter what sourceObject is passed in, the values will always be
        //  the values of the first entry in the sourceObjects list.
        var sourceProperties = ((System.Collections.Generic.IDictionary<string, object>)sourceObject);  

        var targetProperties = typeof(Client).GetProperties().Where(prop => prop.CanWrite);

        var targetObject = new T();

        foreach (var targetProperty in targetProperties)
        {
            if (sourceProperties.ContainsKey(targetProperty.Name))
            {

                var sourceValue = GetProperty(sourceObject, targetProperty.Name);
                targetProperty.SetValue(targetObject, sourceValue, null);
            }               
        }   
        targetList.Add(targetObject);
    }
    return targetList;
}

public static object GetProperty(object o, string member)
{
    if (o == null) throw new ArgumentNullException("o");
    if (member == null) throw new ArgumentNullException("member");
    Type scope = o.GetType();
    IDynamicMetaObjectProvider provider = o as IDynamicMetaObjectProvider;
    if (provider != null)
    {
        ParameterExpression param = Expression.Parameter(typeof(object));
        DynamicMetaObject mobj = provider.GetMetaObject(param);
        GetMemberBinder binder = (GetMemberBinder)Microsoft.CSharp.RuntimeBinder.Binder.GetMember(0, member, scope, new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(0, null) });
        DynamicMetaObject ret = mobj.BindGetMember(binder);
        BlockExpression final = Expression.Block(
            Expression.Label(CallSiteBinder.UpdateLabel),
            ret.Expression
        );
        LambdaExpression lambda = Expression.Lambda(final, param);
        Delegate del = lambda.Compile();
        return del.DynamicInvoke(o);
    }
    else
    {
        return o.GetType().GetProperty(member, BindingFlags.Public | BindingFlags.Instance).GetValue(o, null);
    }
}

它变得更加困难,因为对于返回的动态对象的正常强制转换等只会一遍又一遍地给出列表中的第一个对象。GetProperty 方法可解决此限制。

于 2019-09-17T18:25:28.367 回答