Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
Why can you not use anon function with a dynamic parameter?

Just ran into this today

An anonymous function or method group cannot be used as a constituent value of a dynamically bound operation.

when trying to do

static R ifNotNull<R>(dynamic o, Func<dynamic, R> returnFunc, R otherwise) {
    return ReferenceEquals(null, o) ? otherwise : returnFunc(o);
}

and use it with

dynamic firstAddress = ...;
return ifNotNull<string>(firstAddress, (a) => a.address_1, null)

Now most of the limitations on dynamics make sense to me - you can't use an extension method because how is the compiler supposed to decide which static to compile it to? But I don't get this here. Where does the confusion come in? What exactly is the limitation?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

What is the static type of the lamba a => a.address_1? You may be tempted to say it's a Func<dynamic, dynamic>. But remember:

A lambda expression is an anonymous function that you can use to create delegates or expression tree types.

So maybe it's an Expression<Func<dynamic, dynamic>>. A lamda by itself doesn't have a single static type.

Now normally type inference would figure out that you're passing the lamba to a function that takes a Func and it will be converted to a delegate at compile time. However when you are calling with dynamic arguments the method call is dispatched dynamically.

If you have a method call with a dynamic argument, it is dispatched dynamically, period. During the runtime binding, all the static types of your arguments are known (emphasis mine), and types are picked for the dynamic arguments based on their actual values.

So the fact that your method takes a Func isn't take into account, since the actual method call isn't determined until runtime so there is no type inference.

To get this to compile you'll have to cast your lamba to a Func<dynamic, string> as below:

return ifNotNull<string>(firstAddress, new Func<dynamic, string>((a) => a.address_1), null);

Now the static type of your lamda is known.

over 4 years ago · Santiago Trujillo Denunciar

0

I meant that you need to cast the lambda method to the expect expression you want. Then it'll work just fine.

Like this:

return ifNotNull(firstAddress, (Func<dynamic, string>)((a) => a.address_1), null);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda