Commit 0b032575 authored by Ben Lambell's avatar Ben Lambell

Detect attempts to serialize anonymous methods

parent 18b8d58c
......@@ -7,7 +7,6 @@
*/
using System;
using System.Runtime.Serialization;
using NUnit.Framework;
using Recurse.Persist.Serializables;
......@@ -27,6 +26,14 @@ namespace Recurse.Persist.Test
// means they actually refer to different objects' method calls
}
[Test]
public static void CanSerializeAnonymousDelegates()
{
void anonymousAction(int value) => typeof(InstanceClass).ToString();
Assert.Throws<ArgumentException>(() => new SerializableAction<int>(anonymousAction));
}
[Test]
public static void CanSerializeStaticClassSerializableDelegates()
{
......
......@@ -39,9 +39,12 @@ namespace Recurse.Persist.Serializables
reflectedType.GetMethod(name, ArgumentTypes));
}
public SerializableDelegate(Delegate action)
public SerializableDelegate(Delegate @delegate)
{
Delegate = action;
if (@delegate.Method.Name.Contains("<"))
throw new ArgumentException("Cannot create serializable anonymous delegate.", nameof(@delegate));
Delegate = @delegate;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment