I am trying to get the parameters list of a class method using Roslyn and noticed a strange behavior that Roslyn considers and returns a Lambda parameter used inside the body of the method as as one of the parameters of the method, which cause an error in my code. Why does Roslyn consider a lambda parameter as of the method's parameters?
Here is the code:
var paramDeclaratons = memmeth.DescendantNodes().OfType<ParameterSyntax>();
foreach (var mempara in paramDeclaratons)
{
String paramType = mempara.Type.ToFullString().Trim(); //Here it crashes with System.NullReferenceException because Lambda returns no type!
The code which is parsed:
public void Method1(RequestId requestId)
{
...
var packetsToKeep = this.queuedPackets.Where(p => p.RequestId != requestId)
p is returned as one of the parameters of Method1 with no type
Assuming memmeth is a MethodDeclarationSyntax, then what you want is to access its ParameterList.Parameters:
var paramDeclaratons = memmeth.ParameterList.Parameters;
Related
Let's say I have an object of a certain class A.
Let's say I need an Expression which calls method M on class A.
Is this even doable?
Basically, I need to programatically get this lambda
a => a.M();
The trick is, I want to do this generically, i.e. I plan to use reflection to figure out that the method is called M and what parameters it wants.
I tried using Expression.Lambda(MethodCallExpression method, ParameterExpression params).
The issue there is that when I define the method call expression, I have to specify an instance (or leave it at null if it's a static method, which it isn't). I don't want this. I want whatever the parameter is passed into the lambda (a in my example) to be the instance.
Is this possible?
Yes, it's possible to construct a linq expression at a runtime.
E.g. below is an example of constructing an expression of a method call which returns an object. This is really a dummy example as it's better to avoid object in favor of strict types.
static Expression<Func<T, object>> ComposeMethodCallExpressionAsFuncObject<T>(string methodName)
{
MethodInfo mi = typeof(T).GetMethod(methodName, types: new Type[0])
?? throw new ArgumentException($"There is no '{methodName}' method in the '{typeof(T).Name}' with the empty arguments list!");
var paramExpression = Expression.Parameter(typeof(T));
var methodCallExpression = Expression.Call(paramExpression, mi);
var result = Expression.Lambda<Func<T, object>>(methodCallExpression, paramExpression);
return result; // (T obj) =>obj.methodName()
}
, and example of usage:
int foo = 9988;
var expression = ComposeMethodCallExpressionAsFuncObject<int>(nameof(int.ToString));
//expression: (int obj) => obj.ToString()
var result = expression.Compile()(foo);
Assert.AreEqual("9988", result);
I would like to create the following expression dynamically:
e.Collection.Select(inner => inner.Property)
I created this code to do it, however I have an issue when I execute the expression call, someone knows what I'm doing wrong?
private static Expression InnerSelect<TInnerModel>(IQueryable source, ParameterExpression externalParameter, string complexProperty)
{
// Creates the expression to the external property. // this generates: "e.Collection".
var externalPropertyExpression = Expression.Property(externalParameter, complexProperty);
// Creates the expression to the internal property. // this generates: "inner => inner.Property"
var innerParameter = Expression.Parameter(typeof(TInnerModel), "inner");
var innerPropertyExpression = Expression.Property(innerParameter, "Property");
var innerLambda = Expression.Lambda(innerPropertyExpression, innerParameter);
return Expression.Call(typeof(Queryable), "Select", new [] { typeof(TInnerModel) }, externalPropertyExpression, innerLambda);
}
Error:
No generic method 'Select' on type 'System.Linq.Queryable' is
compatible with the supplied type arguments and arguments. No type
arguments should be provided if the method is non-generic.
So, first off, the primary problem is very simple. As the error message says, you haven't passed enough type arguments to Select. But when you fix that, you'll still have a problem, and that problem will be much harder for you to see and understand.
Let's dig into that.
You wish to represent this as an expression tree:
e.Collection.Select(inner => inner.Property)
Let's begin by rewriting it in its non-extension-method form.
Queryable.Select<A, B>(e.Collection, inner => inner.Property)
Where A is the collection member type and B is the type of Property.
Now, suppose you had this expression in your program. What would it actually do at runtime? It would construct an expression tree for the lambda and pass it to Queryable.Select. That is, it would do something like:
var innerParameter = parameterFactory(whatever);
var lambdaBody = bodyFactory(whatever);
var lambda = makeALambda(lambdaBody, innerParameter);
Queryable.Select<TInnerModel>(e.Collection, lambda);
Right? You with me so far?
Now, suppose we wish to translate this program fragment to an expression tree that could itself be the body of a lambda. That would be:
var theMethodInfoForSelect = whatever;
var receiverE = valueFactory(whatever);
var thePropertyInfoForCollection = whatever;
var theFirstArgument = propertyFactory(receiverE, thePropertyInfoForCollection);
...
Again, with me so far? Now, the crucial question: what is the second argument? It is NOT the value of lambda, which is what you are passing. Remember, what we are doing here is constructing an expression tree which represents the code that the compiler is generating for that thing, and the expression tree that is in lambda is not that. You're mixing levels!
Let me put it this way: the compiler is expecting "add one and three". You are passing 4. Those are very different things! One of them is a description of how to obtain a number and the other one is a number. You are passing an expression tree. What the compiler is expecting is a description of how to obtain an expression tree.
So: do you have to now write code that generates expression trees for all of lambda's construction code? Thank goodness no. We provided you a handy way to turn an expression tree into a description of how to produce an expression tree, which is the Quote operation. You need to use it.
So, what is the right sequence of events that you need to do to build your expression tree? Let's walk through it:
First, you'll need a ParameterExpression of the type of e, which you already have in hand. Let's suppose that is:
ParameterExpression eParam = Expression.Parameter(typeof(E), "e");
Next, you will need a method info for the Select method. Let's suppose you can correctly get that.
MethodInfo selectMethod = whatever;
That method takes two arguments, so let's make an array of argument expressions:
Expression[] arguments = new Expression[2];
You'll need a property info for your Collection property. I assume you can get that:
MethodInfo collectionGetter = whatever;
Now we can build the property expression:
arguments[0] = Expression.Property(eParam, collectionGetter);
Super. Next we need to start building that lambda. We need a parameter info for inner:
ParameterExpression innerParam = Expression.Parameter(typeof(Whatever), "inner");
We'll need a property info for Property, which I assume you can get:
MethodInfo propertyGetter = whatever;
Now we can build the body of the lambda:
MemberExpression body = Expression.Property(innerParam, propertyGetter);
The lambda takes an array of parameters:
ParameterExpression[] innerParams = { innerParam };
Build the lambda from the body and the parameters:
var lambda = Expression.Lambda<Func<X, int>>(body, innerParams);
Now the step you missed. The second argument is the quoted lambda, not the lambda:
arguments[1] = Expression.Quote(lambda);
Now we can build the call to Select:
MethodCallExpression callSelect = Expression.Call(null, selectMethod, arguments);
And we're done.
Give someone an expression tree and you give them an expression tree for a day; teach them how to find expression trees themselves and they can do it for a lifetime. How did I do that so fast?
Since I wrote the expression tree code generator, I had some immediate familiarity with the problem that you were likely to have. But that was ten years ago, and I did not do the above entirely from memory. What I did was I wrote this program:
using System;
using System.Linq.Expressions;
public interface IQ<T> {}
public class E
{
public IQ<X> C { get; set; }
}
public class X
{
public int P { get; set; }
}
public class Program
{
public static IQ<R> S<T, R>(IQ<T> q, Expression<Func<T, R>> f) { return null; }
public static void Main()
{
Expression<Func<E, IQ<int>>> f = e => S<X, int>(e.C, c => c.P);
}
}
Now I wished to know what code was generated by the compiler for the body of the outer lambda, so I went to https://sharplab.io/, pasted in the code, and then clicked on Results --> Decompile C#, which will compile the code to IL and then decompile it back to human-readable C#.
This is the best way I know of to quickly understand what the C# compiler is doing when it builds an expression tree, regardless of whether you know the compiler source code backwards and forwards. It's a very handy tool.
I'm trying to execute a dynamic linq query where my DbSet Type is created at runtime via reflection an I'm getting the error:
"The best overloaded method match for
'System.Linq.Queryable.Where(System.Linq.IQueryable,
System.Linq.Expressions.Expression>)'
has some invalid arguments"
Here's my code
MyDataContext db = new MyDataContext ();
var dbType = db.GetType();
var dbSet = dbType.GetProperty("MyType").GetValue(db,null);
dbSet.GetType().InvokeMember("Local", BindingFlags.GetProperty, null, dbSet , null)
//just to show that it equal
dbSet.Equals(db.MyType); //returns true;
//here i create a dynamic expression tree
dynamic l = Expression.Lambda(delagateType, greater, param);
//here it fails when i pass in my dbSet var but not when i pass db.MyType
dynamic q = ((IEnumerable<object>)Queryable.Where(dbSet , l)).ToList();
The problem is that your dynamic call contains two parameters, the first being static and the second being dynamic. In such case the compiler is using the static type information for the first argument, which is different - object when you pass dbSet variable and DbSet<MyType> when you pass db.MyType.
The trick is to hide the static type information from the compiler like this
dynamic q = ((IEnumerable<object>)Queryable.Where((dynamic)dbSet, l)).ToList();
I am trying to stub a method that has an out paramteter using RhinoMock's Do method, but I keep getting the message cannot resolve symbol outParam. Here's the stubbing part:
private static void FakeClientsLoading(MyClass fakeClass, IEnumerable<string> clientsToLoad)
{
fakeClass.Stub(
x =>
x.LoadClientsFromDb(Arg<string>.Is.Anything,
out Arg<object>.Out(null).Dummy))
.Do(
new LoadClientsFromDbAction(
(someString, out outParam ) =>
TestHelper.LoadClients(someString, clientsToLoad)));
}
And here is my custom delegate declaration:
public delegate void LoadClientsFromDbAction(string s, out object outParam);
What I'd like to achieve is to run the test helper method whenever LoadClientsFromDb is invoked. From my understanding outParam should be mapped to whatever is passed as the out parameter to the called method, but it doesn't seem to work this way.
It seems that I have finally found the answer to my question. It turns out that, quoting section 26.3.1 from this link:
Specifically, a delegate type D is compatible with an anonymous method
or lambda-expression L provided:
If L is a lambda expression that has an implicitly typed parameter list, D has no ref or out parameters.
This means that you need an explicitly typed parameter list in order to create a lambda with an out parameter.
That's not all, though. It is still necessary to assign a value to the out parameter upon exiting the anonymous method.
The final and working code:
private static void FakeClientsLoading(MyClass fakeClass, IEnumerable<string> clientsToLoad)
{
fakeClass.Stub(
x =>
x.LoadClientsFromDb(Arg<string>.Is.Anything,
out Arg<object>.Out(null).Dummy))
.Do(
new LoadClientsFromDbAction(
(string someString, out object outParam) =>
{
outParam = null;
TestHelper.LoadClients(someString, clientsToLoad);
}
));
}
I'm dynamically creating a type from a string passed to me at runtime.
I have the following code at the moment:
string messageType = "CustomerCreatedMessage";
//Convert the string to a Type
var type = Type.GetType(messageType);
//Create a Type of Action<CustomerCreatedMessage>
var action = typeof(Action<>).MakeGenericType(messageType);
How would I make a variable of the type of action and assign the lambda m=> SetActive(true) to it? I will then use the variable (named ??? here) in the following code:
//Create a generic method "Register" on my Messenger class that takes an Action<CustomerCreatedMessage> parameter.
var method = typeof(Messenger).GetMethod("Register");
var genericMethod = method.MakeGenericMethod(action);
//Invoke the created method with our script in it
genericMethod.Invoke(Messenger.Default, ???);
My goal is to be able to call my generic method Messenger.Default.Register<> with an Action<> that takes as type parameter a type created from my messageType string.
I have tried the following already:
Convert.ChangeType(m=>SetActive(true),action);
var filledAction = Activator.CreateInstance(action);
filledAction = m=>SetActive(true);
But both methods complain about converting a lambda expression to an object because it is not a delegate.
I get a similar error if I put SetActive(true) into a method and try to pass the method group.
Given that you're not using the parameter at all, I'd create a new generic method:
private static void GenericSetActive<T>(T ignored)
{
SetActive(true);
}
Then create a delegate using that:
var genericMethod = typeof(Foo).GetMethod("GenericSetActive",
BindingFlags.NonPublic |
BindingFlags.Static);
var concreteMethod = genericMethod.MakeGenericMethod(type);
var actionInstance = Delegate.CreateDelegate(action, concreteMethod);
Basically, you can't use a lambda expression here because you don't have a specific delegate type to convert it to.