-
-
Notifications
You must be signed in to change notification settings - Fork 868
/
Copy pathMemberMethodProvider.cs
66 lines (55 loc) · 2.21 KB
/
MemberMethodProvider.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Reflection;
using SixLabors.ImageSharp.PixelFormats;
using Xunit.Abstractions;
namespace SixLabors.ImageSharp.Tests;
/// <summary>
/// Provides <see cref="Image{TPixel}" /> instances for parametric unit tests.
/// </summary>
/// <typeparam name="TPixel">The pixel format of the image</typeparam>
public abstract partial class TestImageProvider<TPixel> : IXunitSerializable
where TPixel : unmanaged, IPixel<TPixel>
{
private class MemberMethodProvider : TestImageProvider<TPixel>
{
private string declaringTypeName;
private string methodName;
private Func<Image<TPixel>> factoryFunc;
public MemberMethodProvider()
{
}
public MemberMethodProvider(string declaringTypeName, string methodName)
{
this.declaringTypeName = declaringTypeName;
this.methodName = methodName;
}
public override Image<TPixel> GetImage()
{
this.factoryFunc ??= this.GetFactory();
return this.factoryFunc();
}
public override void Serialize(IXunitSerializationInfo info)
{
base.Serialize(info);
info.AddValue(nameof(this.declaringTypeName), this.declaringTypeName);
info.AddValue(nameof(this.methodName), this.methodName);
}
public override void Deserialize(IXunitSerializationInfo info)
{
base.Deserialize(info);
this.methodName = info.GetValue<string>(nameof(this.methodName));
this.declaringTypeName = info.GetValue<string>(nameof(this.declaringTypeName));
}
private Func<Image<TPixel>> GetFactory()
{
var declaringType = Type.GetType(this.declaringTypeName);
MethodInfo m = declaringType.GetMethod(this.methodName);
Type pixelType = typeof(TPixel);
Type imgType = typeof(Image<>).MakeGenericType(pixelType);
Type funcType = typeof(Func<>).MakeGenericType(imgType);
MethodInfo genericMethod = m.MakeGenericMethod(pixelType);
return (Func<Image<TPixel>>)genericMethod.CreateDelegate(funcType);
}
}
}