score:1
i don't understand what you are doing, i think you could probably completely avoid some of the code you show here.
i wrote this "getmembername" extension method, you probably can do something with this code:
public static string getmembername<t, tresult>(
this t anyobject,
expression<func<t, tresult>> expression)
{
return ((memberexpression)expression.body).member.name;
}
// call as extension method, if you have a instance
string lengthpropertyname = "abc".getmembername(x => x.length);
// or call as a static method, by providing the type in the argument
string lengthpropertyname = reflectionutility.getmembername(
(string x) => x.length);
edit:
just to sketch up a solution:
public static bool ismethod<tresult>(
methodinfo method,
expression<func<tresult>> expression)
{
// i think this doesn't work like this, evaluate static method call
return method == ((memberexpression)expression.body).member;
}
if (ismethod(expr.method, () => sqlfilterextensions.like))
{
// generate sql
}
score:0
if you have control over the like method, maybe you could work from there directly instead of inspecting the expressions later.
if you don't have control over the method there is no other way than to do it by comparing the name
score:1
- it wouldn't fail silently if you had unit tests to test it.
- if you used resharper, it would offer to change the text in the string literal at the same time as renaming the method.
Source: stackoverflow.com
Related Query
- Getting MethodInfo without name as string
- C# - code to order by a property using the property name as a string
- ef code first: get entity table name without dataannotations
- C# Linq to XML getting the elements where value of name contains a specific string
- Getting data from a string defined table name filtered on a string defined field
- Getting a column by string name
- List or Array of String Contain specific word in Html Source Code
- How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?
- Is there a LINQ function for getting the longest string in a list of strings?
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Entity Framework - getting a table's column names as a string array
- Select a model property using a lambda and not a string property name
- Call OrderBy() with a field name as a string
- Use string as field name in LINQ
- How to rename the datatable column name without losing the data?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Getting "source" of LINQ query and serialize to string
- Why does getting a member expression member name differ between C# and VB.NET?
- .NET String parsing performance improvement - Possible Code Smell
- linq orderby using property name string
- Any way to find word in string without split
- LINQ member expression getting column name
- LINQ Source Code Available
- Efficient way to unindent lines of code stored in a string
- Sorting using property name as string
- Linq Order by when column name is dynamic and pass as a string to a function
- How to pass a user-input string as a field name to access a field within an object?
- Entity Framework Code First without app.config
- .NET 4 Code Contracts: "requires unproven: source != null"
- The given value of type String from the data source cannot be converted to type int of the specified target column
More Query from same tag
- Partial ungrouping list of duplicate values
- Linq groupby query with projection
- SQL LIKE in Linq
- Get list of Views in Linq
- c# for a group of objects get property and ensure same value for all
- Ordinal Position of Element in IENumerable Collection (Linq to XMl )
- Linq Lambda Expression to return order by total
- autogenerated sql code: single backslash as an escape character is failing
- There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key "Projects"
- join with multiple group by element and select extra columns
- Pass Expression<Func<Type, bool>> as TestCase
- Is possible keep the properties type after using linq selection?
- How to update a list based on more than one Condition using LINQ C#
- Linq how to write a JOIN
- LINQ statement broke after upgrading to .NET Core 1.1 Preview 1
- EF Core return specific columns from child tables
- Why not reveal the type and identity of the source to the client?
- iterate over record in generic list
- LINQ dropping results
- how to combine two fields in linq?
- GroupBy of Anonymous Object strange result
- Compare two lists and add the difference to third list
- LINQ - GroupBy over many-to-many relation
- Creating reusable chunks of LINQ to SQL
- How to fill DataGridView in a MVP project?
- How can I evaluate a deferred Linq statement when debugging?
- LINQ: left join instead of inner join
- How should I get the length of an IEnumerable?
- At least one object must implement IComparable order by
- SQL Except Syntax in C# Linq