6

I have a class which contains a List<Func<T>>. The class is serializable and will be transferred over WCF. Now, I have problems with contained List<Func<T>>. What can I do that this list will also be serializable?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • What kind of problems are you having? – Abe Miessler Oct 18 '12 at 17:58
  • What exactly is the purpose of the list? Is it important that the functions get carried over, or could the object on the other end have an empty list? – Random832 Oct 18 '12 at 17:58
  • Func isn't serializable - how can it be? You'd have to serialize the code, which could have any effect, even an OS call. – James Gaunt Oct 18 '12 at 17:59
  • possible duplicate of [Could we save delegates in a file (C#)](http://stackoverflow.com/questions/1132702/could-we-save-delegates-in-a-file-c), and also [can-you-pass-funct-bool-through-a-wcf-service](http://stackoverflow.com/questions/567316/can-you-pass-funct-bool-through-a-wcf-service) – nawfal Jul 12 '14 at 11:23

1 Answers1

18

How would you expect a Func<T> to be serialized? It's a pointer to a function - such a thing cannot be serialized.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Why not? A function pointer is a piece of data like any other. It would be useful for passing function references between AppDomains for example. – staafl Oct 06 '13 at 18:01
  • The question was why would you _expect_ it to be serializable? Sure, it might be possible under _some_ circumstances, but why expect that there would be a general capability to serialize it. For instance, such a thing would not be useful if you passed it to Java. Why would it necessarily make sense to serialize it as XML in a manner that would make sense to most consumers? – John Saunders Oct 06 '13 at 18:03
  • 4
    I'm sorry, maybe I wasn't clear enough. Obviously it makes no sense to pass a function pointer through a service endpoint. However, binary serialization of delegates is supported, and one might expect it to be, because a) function pointers are data, and b) it's useful for cross-AppDomain work, serializing classes with events, and perhaps more scenarios. – staafl Oct 06 '13 at 18:11