-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathNativeFunctionSignature.cs
More file actions
146 lines (125 loc) · 4.94 KB
/
Copy pathNativeFunctionSignature.cs
File metadata and controls
146 lines (125 loc) · 4.94 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
namespace DllManipulator.Internal
{
internal class NativeFunctionSignature
{
public readonly NativeFunctionParameterSignature returnParameter;
public readonly NativeFunctionParameterSignature[] parameters;
public readonly CallingConvention callingConvention;
public readonly bool bestFitMapping;
public readonly CharSet charSet;
public readonly bool setLastError;
public readonly bool throwOnUnmappableChar;
public NativeFunctionSignature(MethodInfo methodInfo, CallingConvention callingConvention, bool bestFitMapping, CharSet charSet, bool setLastError, bool throwOnUnmappableChar)
{
this.returnParameter = new NativeFunctionParameterSignature(methodInfo.ReturnParameter);
this.parameters = methodInfo.GetParameters().Select(p => new NativeFunctionParameterSignature(p)).ToArray();
this.callingConvention = callingConvention;
this.bestFitMapping = bestFitMapping;
this.charSet = charSet;
this.setLastError = setLastError;
this.throwOnUnmappableChar = throwOnUnmappableChar;
}
public override bool Equals(object obj)
{
var other = obj as NativeFunctionSignature;
if (other == null)
{
return false;
}
if(!returnParameter.Equals(other.returnParameter))
{
return false;
}
if (!parameters.SequenceEqual(other.parameters))
{
return false;
}
if (callingConvention != other.callingConvention)
{
return false;
}
if (bestFitMapping != other.bestFitMapping)
{
return false;
}
if (charSet != other.charSet)
{
return false;
}
if (setLastError != other.setLastError)
{
return false;
}
if (throwOnUnmappableChar != other.throwOnUnmappableChar)
{
return false;
}
return true;
}
public override int GetHashCode()
{
var hashCode = 316391695;
hashCode = hashCode * -1521134295 + returnParameter.GetHashCode();
hashCode = hashCode * -1521134295 + callingConvention.GetHashCode();
hashCode = hashCode * -1521134295 + bestFitMapping.GetHashCode();
hashCode = hashCode * -1521134295 + charSet.GetHashCode();
hashCode = hashCode * -1521134295 + setLastError.GetHashCode();
hashCode = hashCode * -1521134295 + throwOnUnmappableChar.GetHashCode();
return hashCode;
}
}
internal class NativeFunctionParameterSignature
{
public readonly Type type;
public readonly ParameterAttributes parameterAttributes;
public readonly Attribute[] customAttributes;
public NativeFunctionParameterSignature(ParameterInfo parameterInfo)
{
this.type = parameterInfo.ParameterType;
this.parameterAttributes = parameterInfo.Attributes;
var attrs = parameterInfo.GetCustomAttributes(false).OfType<Attribute>(); //XXX: This is required way of obtaining attributes, since both CustomAttributeExtensions.GetCustomAttributes() and Attribute.GetCustomAttributes() return at most 1 attribute (mono bug?)
this.customAttributes = attrs
.Where(a => DllManipulator.SUPPORTED_PARAMATER_ATTRIBUTES.Contains(a.GetType()))
.ToArray();
}
public NativeFunctionParameterSignature(Type type, ParameterAttributes parameterAttributes, Attribute[] customAttributes)
{
this.type = type;
this.parameterAttributes = parameterAttributes;
this.customAttributes = customAttributes;
}
public override bool Equals(object obj)
{
var other = obj as NativeFunctionParameterSignature;
if(other == null)
{
return false;
}
if (type != other.type)
{
return false;
}
if (parameterAttributes != other.parameterAttributes)
{
return false;
}
if (customAttributes.Except(other.customAttributes).Any()) //Check if arrays have the same elements
{
return false;
}
return true;
}
public override int GetHashCode()
{
var hashCode = 424392846;
hashCode = hashCode * -1521134295 + type.GetHashCode();
hashCode = hashCode * -1521134295 + parameterAttributes.GetHashCode();
return hashCode;
}
}
}