|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.Serialization; |
| 7 | + |
| 8 | +namespace NHibernate.Util |
| 9 | +{ |
| 10 | + [Serializable] |
| 11 | + internal sealed class SerializableMethodInfo : ISerializable |
| 12 | + { |
| 13 | + [NonSerialized] |
| 14 | + private readonly MethodInfo _methodInfo; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Creates a new instance of <see cref="SerializableMethodInfo"/> if |
| 18 | + /// <paramref name="methodInfo"/> is not null, otherwise returns <c>null</c>. |
| 19 | + /// </summary> |
| 20 | + /// <param name="methodInfo">The <see cref="MethodInfo"/> being wrapped for serialization.</param> |
| 21 | + /// <returns>New instance of <see cref="SerializableMethodInfo"/> or <c>null</c>.</returns> |
| 22 | + public static SerializableMethodInfo Wrap(MethodInfo methodInfo) |
| 23 | + { |
| 24 | + return methodInfo == null ? null : new SerializableMethodInfo(methodInfo); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Creates a new <see cref="SerializableMethodInfo"/> |
| 29 | + /// </summary> |
| 30 | + /// <param name="methodInfo">The <see cref="MethodInfo"/> being wrapped for serialization.</param> |
| 31 | + public SerializableMethodInfo(MethodInfo methodInfo) |
| 32 | + { |
| 33 | + _methodInfo = methodInfo; |
| 34 | + if (methodInfo != null) |
| 35 | + { |
| 36 | + if (methodInfo.IsStatic) throw new ArgumentException("Only for instance fields", nameof(methodInfo)); |
| 37 | + if (methodInfo.DeclaringType == null) throw new ArgumentException("MethodInfo must have non-null DeclaringType", nameof(methodInfo)); |
| 38 | + |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private SerializableMethodInfo(SerializationInfo info, StreamingContext context) |
| 43 | + { |
| 44 | + System.Type declaringType = info.GetValue<SerializableSystemType>("declaringType")?.GetType(); |
| 45 | + string fieldName = info.GetString("methodName"); |
| 46 | + SerializableSystemType[] parameterSystemTypes = info.GetValue<SerializableSystemType[]>("parameterTypesHelper"); |
| 47 | + |
| 48 | + System.Type[] parameterTypes = parameterSystemTypes?.Select(x => x.GetType()).ToArray() ?? new System.Type[0]; |
| 49 | + this._methodInfo = declaringType?.GetMethod( |
| 50 | + fieldName, |
| 51 | + BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parameterTypes, null); |
| 52 | + } |
| 53 | + |
| 54 | + public void GetObjectData(SerializationInfo info, StreamingContext context) |
| 55 | + { |
| 56 | + SerializableSystemType[] parameterSystemTypes = |
| 57 | + _methodInfo?.GetParameters() |
| 58 | + .Select(x => new SerializableSystemType(x.ParameterType)) |
| 59 | + .ToArray(); |
| 60 | + |
| 61 | + info.AddValueWithType("declaringType", SerializableSystemType.Wrap(_methodInfo?.DeclaringType)); |
| 62 | + info.AddValue("methodName", _methodInfo?.Name); |
| 63 | + info.AddValueWithType("parameterTypesHelper", parameterSystemTypes); |
| 64 | + } |
| 65 | + |
| 66 | + public MethodInfo Value => _methodInfo; |
| 67 | + |
| 68 | + private bool Equals(SerializableMethodInfo other) |
| 69 | + { |
| 70 | + return Equals(_methodInfo, other._methodInfo); |
| 71 | + } |
| 72 | + |
| 73 | + public override bool Equals(object obj) |
| 74 | + { |
| 75 | + if (ReferenceEquals(null, obj)) return false; |
| 76 | + if (ReferenceEquals(this, obj)) return true; |
| 77 | + if (ReferenceEquals(this._methodInfo, obj)) return true; |
| 78 | + if (obj is SerializableMethodInfo) return Equals((SerializableMethodInfo) obj); |
| 79 | + return (obj is MethodInfo) && this._methodInfo.Equals(obj); |
| 80 | + } |
| 81 | + |
| 82 | + public override int GetHashCode() |
| 83 | + { |
| 84 | + return _methodInfo.GetHashCode(); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments