[C#] How can I serialize a Func or Action?

Started by
0 comments, last by a_loho 12 years, 12 months ago
If I have a Ref class:



// Can make a reference type out of value types
public class Ref<T>
{
private Func<T> getter;
private Action<T> setter;

/// <summary>
/// Create a new reference type for value type T
/// </summary>
/// <param name="getter">Use '() => value'</param>
/// <param name="setter">Use 'v => { value = v; }' or 'null' for no setter</param>
public Ref(Func<T> getter, Action<T> setter)
{
this.getter = getter;
this.setter = setter;
}

public T Value
{
get { return getter(); }
set { setter(value); }
}
}


Is there any way to serialize the getter and setter correctly? What are my alternatives if not?
Advertisement
I don't really understand what you are trying to achieve.

If you want to allow null for value type (struct) then you can just use nullable type :

Nullable<int> nullableInt;

// or easier

int? nullableInt;


which support natively serialization.

This topic is closed to new replies.

Advertisement