forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMethodSerialization.cs
More file actions
47 lines (40 loc) · 1.28 KB
/
MethodSerialization.cs
File metadata and controls
47 lines (40 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*using System.IO;
using System.Reflection;
using NUnit.Framework;
using Python.Runtime;
namespace Python.EmbeddingTest.StateSerialization;
public class MethodSerialization
{
[Test]
public void GenericRoundtrip()
{
var method = typeof(MethodTestHost).GetMethod(nameof(MethodTestHost.Generic));
var maybeMethod = new MaybeMethodBase<MethodBase>(method);
var restored = SerializationRoundtrip(maybeMethod);
Assert.IsTrue(restored.Valid);
Assert.AreEqual(method, restored.Value);
}
[Test]
public void ConstrctorRoundtrip()
{
var ctor = typeof(MethodTestHost).GetConstructor(new[] { typeof(int) });
var maybeConstructor = new MaybeMethodBase<MethodBase>(ctor);
var restored = SerializationRoundtrip(maybeConstructor);
Assert.IsTrue(restored.Valid);
Assert.AreEqual(ctor, restored.Value);
}
static T SerializationRoundtrip<T>(T item)
{
using var buf = new MemoryStream();
var formatter = RuntimeData.CreateFormatter();
formatter.Serialize(buf, item);
buf.Position = 0;
return (T)formatter.Deserialize(buf);
}
}
public class MethodTestHost
{
public MethodTestHost(int _) { }
public void Generic<T>(T item, T[] array, ref T @ref) { }
}
*/