Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

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

This topic is 2613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!