Given this method signature:
void Foo<T>(Func<T, object> expression)
Is it possible to use reflection to create a type-representation of Func<dynamic, object>
to use with MakeGenericType
for the expression
argument's type? The "obvious" approaches are not valid C# syntax because dynamic
is neither a type nor an object (ie. typeof(dynamic)
is invalid), so I've not been able to come up with anything useful for the ???
parameter below:
Type fnType = typeof(Func<,>).MakeGenericType(new Type[] { ???, typeof(object) });
I realize dynamic
is a special case in the C# language. I suspect it's effectively a place-holder when I write something like this:
Foo<dynamic>(n => new { n.prop });
Since dynamic
tends to generate a flurry of "your architecture sucks" replies, I'll preempt those by explaining the real-world scenario: I'm using the Roslyn Scripting API to load and compile expression delegates from configuration to filter, destructure, or otherwise alter various objects (including anonymous types, hence dynamic
) written to a structured logger (Serilog).
I am starting to think this is an edge-case that reflection just can't handle. (I've been hoping to avoid Expressions, but I'm wondering if that could pull it off somehow.)
Aucun commentaire:
Enregistrer un commentaire