|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
| 7 | +namespace DllManipulator.Internal |
| 8 | +{ |
| 9 | + internal class NativeFunctionSignature |
| 10 | + { |
| 11 | + public readonly NativeFunctionParameterSignature returnParameter; |
| 12 | + public readonly NativeFunctionParameterSignature[] parameters; |
| 13 | + public readonly CallingConvention callingConvention; |
| 14 | + public readonly bool bestFitMapping; |
| 15 | + public readonly CharSet charSet; |
| 16 | + public readonly bool setLastError; |
| 17 | + public readonly bool throwOnUnmappableChar; |
| 18 | + |
| 19 | + public NativeFunctionSignature(MethodInfo methodInfo, CallingConvention callingConvention, bool bestFitMapping, CharSet charSet, bool setLastError, bool throwOnUnmappableChar) |
| 20 | + { |
| 21 | + this.returnParameter = new NativeFunctionParameterSignature(methodInfo.ReturnParameter); |
| 22 | + this.parameters = methodInfo.GetParameters().Select(p => new NativeFunctionParameterSignature(p)).ToArray(); |
| 23 | + this.callingConvention = callingConvention; |
| 24 | + this.bestFitMapping = bestFitMapping; |
| 25 | + this.charSet = charSet; |
| 26 | + this.setLastError = setLastError; |
| 27 | + this.throwOnUnmappableChar = throwOnUnmappableChar; |
| 28 | + } |
| 29 | + |
| 30 | + public override bool Equals(object obj) |
| 31 | + { |
| 32 | + return obj is NativeFunctionSignature other && |
| 33 | + returnParameter.Equals(other.returnParameter) && |
| 34 | + parameters.SequenceEqual(other.parameters) && |
| 35 | + callingConvention == other.callingConvention && |
| 36 | + bestFitMapping == other.bestFitMapping && |
| 37 | + charSet == other.charSet && |
| 38 | + setLastError == other.setLastError && |
| 39 | + throwOnUnmappableChar == other.throwOnUnmappableChar; |
| 40 | + } |
| 41 | + |
| 42 | + public override int GetHashCode() |
| 43 | + { |
| 44 | + var hashCode = -1225548256; |
| 45 | + hashCode = hashCode * -1521134295 + returnParameter.GetHashCode(); |
| 46 | + hashCode = hashCode * -1521134295 + EqualityComparer<NativeFunctionParameterSignature[]>.Default.GetHashCode(parameters); |
| 47 | + hashCode = hashCode * -1521134295 + callingConvention.GetHashCode(); |
| 48 | + hashCode = hashCode * -1521134295 + bestFitMapping.GetHashCode(); |
| 49 | + hashCode = hashCode * -1521134295 + charSet.GetHashCode(); |
| 50 | + hashCode = hashCode * -1521134295 + setLastError.GetHashCode(); |
| 51 | + hashCode = hashCode * -1521134295 + throwOnUnmappableChar.GetHashCode(); |
| 52 | + return hashCode; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + internal class NativeFunctionParameterSignature |
| 57 | + { |
| 58 | + public readonly Type type; |
| 59 | + public readonly ParameterAttributes parameterAttributes; |
| 60 | + public readonly Attribute[] customAttributes; |
| 61 | + |
| 62 | + public NativeFunctionParameterSignature(ParameterInfo parameterInfo) |
| 63 | + { |
| 64 | + this.type = parameterInfo.ParameterType; |
| 65 | + this.parameterAttributes = parameterInfo.Attributes; |
| 66 | + this.customAttributes = parameterInfo.GetCustomAttributes() |
| 67 | + .Where(a => DllManipulator.SUPPORTED_PARAMATER_ATTRIBUTES.Contains(a.GetType())) |
| 68 | + .ToArray(); |
| 69 | + } |
| 70 | + |
| 71 | + public NativeFunctionParameterSignature(Type type, ParameterAttributes parameterAttributes, Attribute[] customAttributes) |
| 72 | + { |
| 73 | + this.type = type; |
| 74 | + this.parameterAttributes = parameterAttributes; |
| 75 | + this.customAttributes = customAttributes; |
| 76 | + } |
| 77 | + |
| 78 | + public override bool Equals(object obj) |
| 79 | + { |
| 80 | + var other = obj as NativeFunctionParameterSignature; |
| 81 | + return other != null && |
| 82 | + type == other.type && |
| 83 | + parameterAttributes == other.parameterAttributes && |
| 84 | + customAttributes.Except(other.customAttributes).Any(); //Check if arrays have the same elements |
| 85 | + } |
| 86 | + |
| 87 | + public override int GetHashCode() |
| 88 | + { |
| 89 | + var hashCode = 1477582057; |
| 90 | + hashCode = hashCode * -1521134295 + type.GetHashCode(); |
| 91 | + hashCode = hashCode * -1521134295 + EqualityComparer<Attribute[]>.Default.GetHashCode(customAttributes); |
| 92 | + hashCode = hashCode * -1521134295 + customAttributes.GetHashCode(); |
| 93 | + return hashCode; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments