المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Serialization of a Func property



C# Programming
08-08-2011, 02:12 PM
I'd like to serialize this func property of my class.
public Func CostAdditionFunction { get; set; }But if I add the [DataMember(Order = 6)] attribute to use DataContract serialisation, I get the following error:
Type 'System.DelegateSerializationHolder+DelegateEntry' with data contract name 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
Not entirely sure I understand the error, or how to resolve it...

The reason for this request is that there is a related class with a object Cost property, and because I don't know the actual type of Cost, I can't just do Cost + Cost as the object might not have a + operator defined. So the func allows me do things like CostAdditionFunction = (s, t) => ((int) s) + ((int) t);.

btw I did try using Generics to solve this as well, but that didn't work when I was trying to provide the GUI layer for these classes. But if I'm doing something utterly stupid, I'll consider alternatives!