gpt4 book ai didi

linq - LLBLGen TypedConstantExpression 无法转换为 SetExpression

转载 作者:行者123 更新时间:2023-12-02 03:46:56 28 4
gpt4 key购买 nike

我们在 contains LINQ 表达式中使用超过 2100 个元素时遇到问题,因此我重写了查询以将之前在 contains 比较中使用的值插入到 IEnumerable 类型的 EnquiryID 结构中,公开一个简单的 int 值 (ID)并加入了这个:

IEnumerable<EnquiryID> enqIdList = ListToEnquiryIdList(enquiryIDs).ToList();

var extras = (from q in lmd.Quote
join qe in lmd.QuoteExtra on q.Id equals qe.QuoteId
join ei in enqIdList on q.EnquiryId equals ei.Id
orderby qe.Created
select new
{
EnquiryID = q.EnquiryId, qe.Created
}).ToArray();

这会产生这个异常:

“无法将‘System.Linq.Expressions.TypedConstantExpression’类型的对象转换为类型‘SD.LLBLGen.Pro.LinqSupportClasses.ExpressionClasses.SetExpression’。”,这显然是 LLBLGEN 特定的

有人有什么想法吗?

最佳答案

您可以像这样访问 TypedConstantExpression 的“Value”属性:
(expression.Body as MethodCallExpression).Object.GetType().GetProperty("Value").GetMethod.Invoke((expression.Body as MethodCallExpression).Object, null)

其中表达式由 LambdaExpression ()=>(null).GetType()

一个完整的例子

static readonly Type TypedConstantExpressionType = Type.GetType("System.Linq.Expressions.TypedConstantExpression, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

static readonly PropertyInfo TypedConstantExpressionValueProperty;

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized | System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
static SymbolExtensions()
{
TypedConstantExpressionValueProperty = IntrospectionExtensions.GetTypeInfo(TypedConstantExpressionType).GetProperty("Value");
}

/// <summary>
/// Given a lambda expression that expressed a new object, returns the <see cref="System.Reflection.TypeInfo"/> of what type was expected to be allocated
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static TypeInfo GetTypeInfo(Expression<Action> expression) //Expression<Action> allows the syntax () => where Expression would require a Delgate.
{
Expression body = expression.Body;

if (body is NewExpression)
{
NewExpression newExpression = expression.Body as NewExpression;

return IntrospectionExtensions.GetTypeInfo((expression.Body as NewExpression).Constructor.DeclaringType);
}
else if (body is MemberExpression)
{
MemberExpression memberExpression = body as MemberExpression;

return IntrospectionExtensions.GetTypeInfo(memberExpression.Member.DeclaringType);
}
else if (body is MethodCallExpression)
{
MethodCallExpression methodCallExpression = expression.Body as MethodCallExpression;

if (methodCallExpression.Object is MemberExpression)
{
return IntrospectionExtensions.GetTypeInfo((methodCallExpression.Object as MemberExpression).Member.DeclaringType);
}

//Actually a RuntimeType from a TypedConstantExpression...
return IntrospectionExtensions.GetTypeInfo((Type)TypedConstantExpressionValueProperty.GetMethod.Invoke(methodCallExpression.Object, null));
}

throw new System.NotSupportedException("Please create an issue for your use case.");
}

使用以下类和代码进行测试:

public class MyTestClass
{
string m_Test;

public string Test
{
get { return m_Test; }
set { m_Test = value; }
}
}

public class MyTestClass<T> : MyTestClass
{
T Backing;

public T Property
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get { return Backing; }
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
set { Backing = value; }
}

public T AnotherProperty
{
get;
set;
}
}



System.Reflection.TypeInfo typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => new MyTestClass<int>());

if (typeInfo.GetGenericArguments()[0] != typeof(int)) throw new System.Exception("Not correct type");

System.Console.WriteLine("TypeInfo.Name" + typeInfo.Name);

System.Console.WriteLine("TypeInfo.MetadataToken" + typeInfo.MetadataToken);

if (typeInfo.GetGenericArguments()[0] != typeof(int)) throw new System.Exception("Not correct type");

typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => (typeof(MyTestClass<int>)).GetType());

if (typeInfo.GetGenericArguments()[0] != typeof(int)) throw new System.Exception("Not correct type");

System.Type unboundedType = typeof(MyTestClass<>);

typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => (unboundedType).GetType());

System.Console.WriteLine("TypeInfo.Name" + typeInfo.Name);

System.Console.WriteLine("TypeInfo.MetadataToken" + typeInfo.MetadataToken);

关于linq - LLBLGen TypedConstantExpression 无法转换为 SetExpression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16420568/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com