110,499
社区成员
发帖
与我相关
我的任务
分享
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class teatca
{
public string A { get; set; }
public string B { get; set; }
}
public class testc
{
public List<teatca> As { get; set; }
}
class Program
{
static void Main(string[] args)
{
testc cc = new testc();
var bb = cc.As;
var bbwhere = bb.Where(x => x.A == "aaa");
//第一个集合通过查询资料已经解决了,第二个实在是在网上查询不到解决方案才敢叨扰各位大神!
var bb1 = cc.GetType().GetProperty("As").GetValue(cc);
//我知道下面的写法不对,我就是想用反射的方法来实现linq语句,实现where 求各位大神!!!
//我就想让bbwhere1的值和bbwhere一样就可以
var bbwhere1 = bb1.GetType().GetGenericArguments().Where(x => x.GetProperty("A").GetValue(x).ToString() == "aaa");
Console.ReadLine();
}
}
}
private static System.Reflection.MethodInfo getExpressionLambdaMethod()
{
foreach (var method in typeof(System.Linq.Expressions.Expression).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public))
{
if (method.IsGenericMethodDefinition && method.Name == "Lambda" && method.GetGenericArguments().Length == 1)
{
var methodParameters = method.GetParameters();
if (methodParameters.Length == 2 && methodParameters[0].ParameterType == typeof(System.Linq.Expressions.Expression) && methodParameters[1].ParameterType == typeof(System.Linq.Expressions.ParameterExpression[]))
{
return method;
}
}
}
throw new MissingMethodException();
}
作者:肖进
链接:https://www.zhihu.com/question/63343132/answer/208187509
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
private static System.Reflection.MethodInfo getEnumerableWhereMethod()
{
foreach (var method in typeof(System.Linq.Enumerable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public))
{
if (method.IsGenericMethodDefinition && method.Name == "Where" && method.GetGenericArguments().Length == 1)
{
var methodParameters = method.GetParameters();
if (methodParameters.Length == 2 && methodParameters[0].ParameterType.IsGenericType && methodParameters[1].ParameterType.IsGenericType && methodParameters[0].ParameterType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IEnumerable<>) && methodParameters[1].ParameterType.GetGenericTypeDefinition() == typeof(Func<,>))
{
return method;
}
}
}
throw new MissingMethodException();
}
作者:肖进
链接:https://www.zhihu.com/question/63343132/answer/208187509
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
var bb1 = cc.GetType().GetProperty("As").GetValue(cc);
Type[] types = bb1.GetType().GetGenericArguments();
Type funcType = typeof(Func<,>).MakeGenericType(types[0], typeof(bool));
System.Linq.Expressions.ParameterExpression[] parameters = new System.Linq.Expressions.ParameterExpression[] { System.Linq.Expressions.Expression.Parameter(types[0], "value") };
var expression = getExpressionLambdaMethod().MakeGenericMethod(funcType).Invoke(null, new object[] { System.Linq.Expressions.Expression.Equal(System.Linq.Expressions.Expression.Property(parameters[0], types[0].GetProperty("A").GetGetMethod()), System.Linq.Expressions.Expression.Constant("aaa", typeof(string))), parameters });
var func = typeof(System.Linq.Expressions.Expression<>).MakeGenericType(funcType).GetMethod("Compile", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, new Type[0], null).Invoke(expression, null);
var bbwhere1 = getEnumerableWhereMethod().MakeGenericMethod(types).Invoke(null, new object[] { bb1, func });
static void Main(string[] args)
{
testc cc = new testc();
cc.As = new List<teatca>() { new teatca() { A = "aaa", B = "2" } };
var bb = cc.As;
var bbwhere = bb.Where(x => x.A == "aaa");
//第一个集合通过查询资料已经解决了,第二个实在是在网上查询不到解决方案才敢叨扰各位大神!
dynamic bb1 = cc.GetType().GetProperty("As").GetValue(cc);
//我知道下面的写法不对,我就是想用反射的方法来实现linq语句,实现where 求各位大神!!!
//我就想让bbwhere1的值和bbwhere一样就可以
foreach (var item in bb1)
{
var r = item.GetType().GetProperty("A").GetValue(item);
Console.WriteLine(r);
}
Console.ReadLine();
}