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