다음 링크는 동적 linq 쿼리에 대한 정보를 제공합니다. DLL에 대한 반성이 도움이 될 수도 있습니다. http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
궁극적으로 System.Linq.Expressions.Expression에서 정적 팩터 리 방법을 사용해야 식을 작성할 수 있습니다.
나는이 가정 여기합니다
- 당신은 정말 문자열의 = mA + MB의
- A와 B는 속성이 아닌 필드 여기
모두 원하는 것은 설명하기 위해 테스트 클래스 내 예 :
public class TestClass
{
public string A { get; set; }
public string B { get; set; }
}
다음은 람다를 빌드하는 정적 클래스 및 메소드입니다. 형식 < Func < TestClass 문자열 > >으로 변환 한 다음 모든 메서드 호출과 마찬가지로 실행할 수있는 > 문자열 인 Func < TestClass로 컴파일합니다.
public static class TestHelper
{
public static string GetString()
{
var testObject = new TestClass { A = "Hey", B = "Man" }; //Replace TestClass
var type = testObject.GetType();
var parameter = Expression.Parameter(type, "m");
var property1 = Expression.Property(parameter, "A");
var property2 = Expression.Property(parameter, "B");
MethodInfo mi = typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) });
var concat = Expression.Call(mi, property1, property2);
var lambda = Expression.Lambda<Func<TestClass, string>>(concat, parameter); //Replace TestClass
var newFunction = lambda.Compile();
string s = newFunction(testObject); //Replace TestClass
}
}