Note that there are some explanatory texts on larger screens.

plurals
  1. POExpression Tree with string assignment and getting value
    primarykey
    data
    text
    <p>I have built my own SQL Query builder that breaks apart an Expression, however, I'm having an issue trying to get the value of string defined in the same function as the lambda expression.</p> <p>Here is what I am trying to do in console app:</p> <pre><code>private static void MyBuilderTest() { var sqlBuilder = new SqlBuilder(); // Doesn't work -- NEED GUIDANCE HERE var testValue = "Test"; // Defined in the same function as the lambda below sqlBuilder.Select&lt;FooObject&gt;(o =&gt; o.FooValue == testValue); // Works var someObject = new SomeObject { SomeValue = "classTest }; sqlBuilder.Select&lt;FooObject&gt;(o =&gt; o.FooValue == someObject.SomeValue); } </code></pre> <p>In my builder it subclasses from ExpressionVisitor, and I override the VisitMember. I found that a string defined in at the base Console level will come back as:</p> <pre><code>Node.Expression.NodeType == ExpressionType.Constant </code></pre> <p>The Node.Expression passes back properties of:</p> <pre><code>CanReduce = false DebugView = ".Constant&lt;ConsoleApplication1.Program+&lt;&gt;c__DisplayClass1&gt;(ConsoleApplication1.Program+&lt;&gt;c__DisplayClass1)" NodeType = Constant Type = System.Type {System.RunetimeType} Value = {ConsoleApplication1.Program} </code></pre> <p>The Node.Expression.Value contains:</p> <pre><code>testValue = "Test" (Type: string) </code></pre> <p><strong>How do I get this value?</strong> I've tried several things, like:</p> <pre><code>var memberType = node.Expression.Type.DeclaringType; </code></pre> <p>This passes back a ConsoleApplication1.Program type.</p> <p>However, when I do:</p> <pre><code> memberType.GetProperty("testValue"); // Declaring Type from Expression </code></pre> <p>It passes back null.</p> <p>The above methods work fine if I place the lambda "strings" in a class, but doesn't work if they string is defined in the console function.</p> <p>Can anyone tell me how to get the string value if it's defined at the function level of the lambda?</p> <p><strong>EDITED: Added VisitMember</strong></p> <pre><code>protected override Expression VisitMember(MemberExpression node) { if (node.NodeType == ExpressionType.Constant) { // Node.Expression is a ConstantExpression type. // node.Expression contains properties above // And Has Value of: {ConsoleApplication1.Program} // Expanding Value in Watch window shows: testValue = "Test" // How do I get this value, if the ConsoleApplication1.Program type doesn't // even know about it? Looks like maybe a dynamic property? } } </code></pre> <p><strong>EDITED</strong></p> <p>Added code to the console app example to show what works and what doesn't.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload