-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDynamicLibraryFunction.cs
More file actions
103 lines (92 loc) · 3.48 KB
/
DynamicLibraryFunction.cs
File metadata and controls
103 lines (92 loc) · 3.48 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using BytecodeApi.Extensions;
using System.Diagnostics;
using System.Reflection;
namespace BytecodeApi.Interop;
/// <summary>
/// Represents the function of a native DLL file that does not return a value.
/// </summary>
[DebuggerDisplay($"{nameof(DynamicLibraryFunction)}: {{Name,nq}}({{DebuggerDisplayParameters,nq}})")]
public sealed class DynamicLibraryFunction
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplayParameters => Method.GetParameters().Select(parameter => parameter.ParameterType.ToCSharpName()).AsString(", ");
private readonly MethodInfo Method;
/// <summary>
/// Gets the <see cref="DynamicLibrary" /> that was used to create this <see cref="DynamicLibraryFunction" />.
/// </summary>
public DynamicLibrary Library { get; }
/// <summary>
/// Gets the name of the entry point in the DLL.
/// </summary>
public string Name => Method.Name;
internal DynamicLibraryFunction(DynamicLibrary library, MethodInfo method)
{
Library = library;
Method = method;
}
/// <summary>
/// Calls the function with the specified parameters.
/// </summary>
/// <param name="parameters">A collection of parameters. The number of parameters must match the number of parameter types upon creation.</param>
public void Call(params object?[]? parameters)
{
Check.TargetParameterCount(parameters?.Length ?? 0, Method.GetParameters().Length);
Method.Invoke(null, parameters);
}
/// <summary>
/// Returns the name of this <see cref="DynamicLibraryFunction" />.
/// </summary>
/// <returns>
/// The name of this <see cref="DynamicLibraryFunction" />.
/// </returns>
public override string ToString()
{
return Name;
}
}
/// <summary>
/// Represents the function of a native DLL file that returns a value of the specified type.
/// </summary>
/// <typeparam name="T">The function's return type.</typeparam>
[DebuggerDisplay($"{nameof(DynamicLibraryFunction<T>)}: {{Name,nq}}({{DebuggerDisplayParameters,nq}})")]
public sealed class DynamicLibraryFunction<T>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplayParameters => Method.GetParameters().Select(parameter => parameter.ParameterType.ToCSharpName()).AsString(", ");
private readonly MethodInfo Method;
/// <summary>
/// Gets the <see cref="DynamicLibrary" /> that was used to create this <see cref="DynamicLibraryFunction{T}" />.
/// </summary>
public DynamicLibrary Library { get; }
/// <summary>
/// Gets the name of the entry point in the DLL.
/// </summary>
public string Name => Method.Name;
internal DynamicLibraryFunction(DynamicLibrary library, MethodInfo method)
{
Library = library;
Method = method;
}
/// <summary>
/// Calls the function with the specified parameters and returns a value.
/// </summary>
/// <param name="parameters">A collection of parameters. The number of parameters must match the number of parameter types upon creation.</param>
/// <returns>
/// The value that the native function returned.
/// </returns>
public T? Call(params object?[]? parameters)
{
Check.TargetParameterCount(parameters?.Length ?? 0, Method.GetParameters().Length);
return Method.Invoke<T>(null, parameters);
}
/// <summary>
/// Returns the name of this <see cref="DynamicLibraryFunction{T}" />.
/// </summary>
/// <returns>
/// The name of this <see cref="DynamicLibraryFunction{T}" />.
/// </returns>
public override string ToString()
{
return Name;
}
}