3

Suppose I have a function DoStuff that is defined in the assembly A.dll. This function takes a Func<T> parameter. I call this function inside of B.dll as DoStuff(() => ...).

Where is the Func<T> evaluated? It appears to be inside of B.dll, but I would expect it to be inside of A.dll. What happens if the Func<T> requires external references? Am I forced to include them in B.dll? Can I force it to execute inside A.dll?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
bbush
  • 45
  • 8
  • What is the requirement, where you are forecd to make such complex implemenation in a real life project, is it possible for you to reveal something more, asking out of curiosity? – Anil Feb 03 '16 at 14:13
  • It's more of a design decision. I have a WCF service that I want to make calls against. I defined a wrapper method in a separate assembly that takes a `Func` and captures any exceptions to wrap them into a `Response` object I defined. The problem is that I am forced to include extra assemblies wherever I make calls to this method in separate assemblies. – bbush Feb 03 '16 at 14:20
  • @bbush perhaps this may help you: http://stackoverflow.com/questions/6443433/how-can-i-pass-a-lambda-expression-to-a-wcf-service – Daniel A. White Feb 03 '16 at 14:28

2 Answers2

3

The lambda is compiled into B and executed there.

You can read up more on how it works here: What are C# lambda's compiled into? A stackframe, an instance of an anonymous type, or?

Community
  • 1
  • 1
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Great, thank you. Can you think of how I might achieve something like this? I would ideally want the compilation to occur in A. I fiddled with expression trees, which works, technically, but these are severely [limited](http://csharpindepth.com/ViewNote.aspx?NoteID=68). – bbush Feb 03 '16 at 14:26
  • @bbush that doesn't make much sense to do that. perhaps you can explain/expand that in a new question that would help. – Daniel A. White Feb 03 '16 at 14:27
  • Sure thing. I asked [this](http://stackoverflow.com/q/34904616/3494973) earlier with no repsonse. – bbush Feb 03 '16 at 14:36
3

Where is the Func evaluated?

You are passing the function definition from B to A. The compiled code exists in B. A presumably executes the function as part of DoStuff.

What happens if the Func<T> requires external references?

Then those dependencies will need to be referenced by project B and will need to get included in the final build, which may mean referencing them from the executing project (the WCF service from your comments) as well.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Great, thank you. Can you think of how I might achieve something like this? I would ideally want the compilation to occur in A. I fiddled with expression trees, which works, technically, but these are severely limited. – bbush Feb 03 '16 at 14:28
  • 1
    Why do you care where the compiled function is? What difference does it make? – D Stanley Feb 03 '16 at 14:30
  • It requires extra dependencies depending on where it is compiled. I'd prefer to allow consumers of the A.dll to not require any extra effort. – bbush Feb 03 '16 at 14:33
  • `A` should not have to reference the dependencies unless it _directly_ uses them. Only the project that uses classes/methods from the dependent assembly and the ultimate project (app, web site, service, etc.) should need to reference the dependencies. – D Stanley Feb 03 '16 at 14:36