diff --git a/src/NHibernate.Test/MappingExceptions/PropertyNotFoundExceptionFixture.cs b/src/NHibernate.Test/MappingExceptions/PropertyNotFoundExceptionFixture.cs index 211a79e7cdf..639800cac58 100644 --- a/src/NHibernate.Test/MappingExceptions/PropertyNotFoundExceptionFixture.cs +++ b/src/NHibernate.Test/MappingExceptions/PropertyNotFoundExceptionFixture.cs @@ -1,5 +1,6 @@ using System; using NHibernate.Cfg; +using NHibernate.Util; using NUnit.Framework; namespace NHibernate.Test.MappingExceptions @@ -44,5 +45,21 @@ public void ConstructWithNullType() new PropertyNotFoundException(null, "someField"); new PropertyNotFoundException(null, "SomeProperty", "getter"); } + + [Test] + public void IsSerializable() + { + NHAssert.IsSerializable(new PropertyNotFoundException(null, "someField")); + NHAssert.IsSerializable(new PropertyNotFoundException(null, "SomeProperty", "getter")); + } + + [Test] + public void SerializeWithType() + { + var bytes = SerializationHelper.Serialize(new PropertyNotFoundException(typeof(PropertyNotFoundExceptionFixture), "SomeProperty", "getter")); + var pnfe = (PropertyNotFoundException) SerializationHelper.Deserialize(bytes); + + Assert.That(pnfe.TargetType, Is.EqualTo(typeof(PropertyNotFoundExceptionFixture))); + } } -} \ No newline at end of file +} diff --git a/src/NHibernate/Bytecode/UnableToLoadProxyFactoryFactoryException.cs b/src/NHibernate/Bytecode/UnableToLoadProxyFactoryFactoryException.cs index d432c6310b2..fc9bbe48284 100644 --- a/src/NHibernate/Bytecode/UnableToLoadProxyFactoryFactoryException.cs +++ b/src/NHibernate/Bytecode/UnableToLoadProxyFactoryFactoryException.cs @@ -1,20 +1,39 @@ using System; using System.Runtime.Serialization; +using System.Security; namespace NHibernate.Bytecode { [Serializable] public class UnableToLoadProxyFactoryFactoryException : HibernateByteCodeException { - private readonly string typeName; + public UnableToLoadProxyFactoryFactoryException(string typeName, Exception inner) : base("", inner) { - this.typeName = typeName; + TypeName = typeName; + } + + protected UnableToLoadProxyFactoryFactoryException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + foreach (var entry in info) + { + if (entry.Name == "TypeName") + { + TypeName = entry.Value?.ToString(); + } + } + } + + [SecurityCritical] + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("TypeName", TypeName); } - protected UnableToLoadProxyFactoryFactoryException(SerializationInfo info, - StreamingContext context) : base(info, context) {} + public string TypeName { get; } public override string Message { get @@ -28,10 +47,10 @@ public override string Message Confirm that your deployment folder contains one of the following assemblies: NHibernate.ByteCode.LinFu.dll NHibernate.ByteCode.Castle.dll"; - string msg = "Unable to load type '" + typeName + "' during configuration of proxy factory class." + causes; + string msg = "Unable to load type '" + TypeName + "' during configuration of proxy factory class." + causes; return msg; } } } -} \ No newline at end of file +} diff --git a/src/NHibernate/DuplicateMappingException.cs b/src/NHibernate/DuplicateMappingException.cs index 415c3bfe2bf..b510b23b5e6 100644 --- a/src/NHibernate/DuplicateMappingException.cs +++ b/src/NHibernate/DuplicateMappingException.cs @@ -1,29 +1,12 @@ using System; using System.Runtime.Serialization; +using System.Security; namespace NHibernate { [Serializable] public class DuplicateMappingException : MappingException { - private readonly string type; - private readonly string name; - - /// - /// The type of the duplicated object - /// - public string Type - { - get { return type; } - } - - /// - /// The name of the duplicated object - /// - public string Name - { - get { return name; } - } /// /// Initializes a new instance of the class. @@ -34,8 +17,8 @@ public string Name public DuplicateMappingException(string customMessage, string type, string name) : base(customMessage) { - this.type = type; - this.name = name; + Type = type; + Name = name; } /// @@ -44,7 +27,7 @@ public DuplicateMappingException(string customMessage, string type, string name) /// The name of the duplicate object /// The type of the duplicate object public DuplicateMappingException(string type, string name) - : this(string.Format("Duplicate {0} mapping {1}", type, name), type, name) + : this($"Duplicate {type} mapping {name}", type, name) { } @@ -62,6 +45,35 @@ public DuplicateMappingException(string type, string name) public DuplicateMappingException(SerializationInfo info, StreamingContext context) : base(info, context) { + foreach (var entry in info) + { + if (entry.Name == "Type") + { + Type = entry.Value?.ToString(); + } + else if (entry.Name == "Name") + { + Name = entry.Value?.ToString(); + } + } } + + [SecurityCritical] + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("Type", Type); + info.AddValue("Name", Name); + } + + /// + /// The type of the duplicated object + /// + public string Type { get; } + + /// + /// The name of the duplicated object + /// + public string Name { get; } } -} \ No newline at end of file +} diff --git a/src/NHibernate/Exceptions/ConstraintViolationException.cs b/src/NHibernate/Exceptions/ConstraintViolationException.cs index cee757168b3..0e933f3b4f3 100644 --- a/src/NHibernate/Exceptions/ConstraintViolationException.cs +++ b/src/NHibernate/Exceptions/ConstraintViolationException.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.Serialization; +using System.Security; namespace NHibernate.Exceptions { @@ -10,29 +11,41 @@ namespace NHibernate.Exceptions [Serializable] public class ConstraintViolationException : ADOException { - private readonly string constraintName; - public ConstraintViolationException(SerializationInfo info, StreamingContext context) - : base(info, context) {} - public ConstraintViolationException(string message, Exception innerException, string sql, string constraintName) : base(message, innerException, sql) { - this.constraintName = constraintName; + ConstraintName = constraintName; } public ConstraintViolationException(string message, Exception innerException, string constraintName) : base(message, innerException) { - this.constraintName = constraintName; + ConstraintName = constraintName; + } + + public ConstraintViolationException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + foreach (var entry in info) + { + if (entry.Name == "ConstraintName") + { + ConstraintName = entry.Value?.ToString(); + } + } + } + + [SecurityCritical] + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("ConstraintName", ConstraintName); } /// /// Returns the name of the violated constraint, if known. /// /// The name of the violated constraint, or null if not known. - public string ConstraintName - { - get { return constraintName; } - } + public string ConstraintName { get; } } } diff --git a/src/NHibernate/Exceptions/SqlParseException.cs b/src/NHibernate/Exceptions/SqlParseException.cs index 4cc33828a53..615d7dbed0b 100644 --- a/src/NHibernate/Exceptions/SqlParseException.cs +++ b/src/NHibernate/Exceptions/SqlParseException.cs @@ -1,13 +1,18 @@ using System; +using System.Runtime.Serialization; namespace NHibernate.Exceptions { - public class SqlParseException : Exception - { + [Serializable] + public class SqlParseException : Exception + { - public SqlParseException(string Message) : base(Message) - { - } + public SqlParseException(string message) : base(message) + { + } - } + protected SqlParseException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } } diff --git a/src/NHibernate/Hql/Ast/ANTLR/DetailedSemanticException.cs b/src/NHibernate/Hql/Ast/ANTLR/DetailedSemanticException.cs index 03664765475..4e01696df12 100644 --- a/src/NHibernate/Hql/Ast/ANTLR/DetailedSemanticException.cs +++ b/src/NHibernate/Hql/Ast/ANTLR/DetailedSemanticException.cs @@ -1,7 +1,9 @@ using System; +using System.Runtime.Serialization; namespace NHibernate.Hql.Ast.ANTLR { + [Serializable] public class DetailedSemanticException : SemanticException { public DetailedSemanticException(string message) : base(message) @@ -12,5 +14,9 @@ public DetailedSemanticException(string message, Exception inner) : base(message, inner) { } + + protected DetailedSemanticException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } } } diff --git a/src/NHibernate/Hql/Ast/ANTLR/InvalidPathException.cs b/src/NHibernate/Hql/Ast/ANTLR/InvalidPathException.cs index 4e761196b29..6ca3a9b75c4 100644 --- a/src/NHibernate/Hql/Ast/ANTLR/InvalidPathException.cs +++ b/src/NHibernate/Hql/Ast/ANTLR/InvalidPathException.cs @@ -1,14 +1,22 @@ -namespace NHibernate.Hql.Ast.ANTLR +using System; +using System.Runtime.Serialization; + +namespace NHibernate.Hql.Ast.ANTLR { /// /// Exception thrown when an invalid path is found in a query. /// Author: josh /// Ported by: Steve Strong /// + [Serializable] public class InvalidPathException : SemanticException { public InvalidPathException(string s) : base(s) { } + + protected InvalidPathException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } } } diff --git a/src/NHibernate/Hql/Ast/ANTLR/SemanticException.cs b/src/NHibernate/Hql/Ast/ANTLR/SemanticException.cs index 47959d0cab6..e30b7aabb4b 100644 --- a/src/NHibernate/Hql/Ast/ANTLR/SemanticException.cs +++ b/src/NHibernate/Hql/Ast/ANTLR/SemanticException.cs @@ -1,8 +1,9 @@ using System; -using Antlr.Runtime; +using System.Runtime.Serialization; namespace NHibernate.Hql.Ast.ANTLR { + [Serializable] public class SemanticException : QueryException { public SemanticException(string message) : base(message) @@ -13,5 +14,9 @@ public SemanticException(string message, Exception inner) : base(message, inner) { } + + protected SemanticException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } } } diff --git a/src/NHibernate/Hql/QueryExecutionRequestException.cs b/src/NHibernate/Hql/QueryExecutionRequestException.cs index 5b038a5c3e6..c471d5f55ce 100644 --- a/src/NHibernate/Hql/QueryExecutionRequestException.cs +++ b/src/NHibernate/Hql/QueryExecutionRequestException.cs @@ -1,11 +1,17 @@ using System; +using System.Runtime.Serialization; namespace NHibernate.Hql { + [Serializable] public class QueryExecutionRequestException : QueryException { public QueryExecutionRequestException(string message, string queryString) : base(message, queryString) { } + + protected QueryExecutionRequestException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } } -} \ No newline at end of file +} diff --git a/src/NHibernate/LazyInitializationException.cs b/src/NHibernate/LazyInitializationException.cs index fc4319f8f35..967e11ee653 100644 --- a/src/NHibernate/LazyInitializationException.cs +++ b/src/NHibernate/LazyInitializationException.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.Serialization; +using System.Security; namespace NHibernate @@ -18,14 +19,14 @@ public class LazyInitializationException : HibernateException /// The id of the entity where the exception was thrown /// The message that describes the error. public LazyInitializationException(string entityName, object entityId, string message) - : this(string.Format("Initializing[{0}#{1}]-{2}", entityName, entityId, message)) + : this($"Initializing[{entityName}#{entityId}]-{message}") { EntityName = entityName; EntityId = entityId; } - public string EntityName { get; private set; } - public object EntityId { get; private set; } + public string EntityName { get; } + public object EntityId { get; } /// /// Initializes a new instance of the class. @@ -76,6 +77,25 @@ public LazyInitializationException(string message, Exception innerException) : b /// protected LazyInitializationException(SerializationInfo info, StreamingContext context) : base(info, context) { + foreach (var entry in info) + { + if (entry.Name == "EntityName") + { + EntityName = entry.Value?.ToString(); + } + else if (entry.Name == "EntityId") + { + EntityId = entry.Value; + } + } + } + + [SecurityCritical] + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("EntityName", EntityName); + info.AddValue("EntityId", EntityId, typeof(object)); } } -} \ No newline at end of file +} diff --git a/src/NHibernate/PropertyNotFoundException.cs b/src/NHibernate/PropertyNotFoundException.cs index cc3c0150ce9..d64108eaae3 100644 --- a/src/NHibernate/PropertyNotFoundException.cs +++ b/src/NHibernate/PropertyNotFoundException.cs @@ -1,5 +1,7 @@ using System; using System.Runtime.Serialization; +using System.Security; +using NHibernate.Util; namespace NHibernate { @@ -9,10 +11,6 @@ namespace NHibernate [Serializable] public class PropertyNotFoundException : MappingException { - private readonly System.Type targetType; - private readonly string propertyName; - private readonly string accessorType; - /// /// Initializes a new instance of the class, /// used when a property get/set accessor is missing. @@ -22,13 +20,11 @@ public class PropertyNotFoundException : MappingException /// The type of the missing accessor /// ("getter" or "setter") public PropertyNotFoundException(System.Type targetType, string propertyName, string accessorType) - : base(String.Format("Could not find a {0} for property '{1}' in class '{2}'", - accessorType, propertyName, targetType - )) + : base($"Could not find a {accessorType} for property '{propertyName}' in class '{targetType}'") { - this.targetType = targetType; - this.propertyName = propertyName; - this.accessorType = accessorType; + TargetType = targetType; + PropertyName = propertyName; + AccessorType = accessorType; } /// @@ -38,20 +34,18 @@ public PropertyNotFoundException(System.Type targetType, string propertyName, st /// The that is missing the field /// The name of the missing property public PropertyNotFoundException(System.Type targetType, string propertyName) - : base(String.Format("Could not find property nor field '{0}' in class '{1}'", - propertyName, targetType)) + : base($"Could not find property nor field '{propertyName}' in class '{targetType}'") { - this.targetType = targetType; - this.propertyName = propertyName; + TargetType = targetType; + PropertyName = propertyName; } public PropertyNotFoundException(string propertyName, string fieldName, System.Type targetType) - : base(String.Format("Could not find the property '{0}', associated to the field '{1}', in class '{2}'", propertyName, fieldName, targetType - )) + : base($"Could not find the property '{propertyName}', associated to the field '{fieldName}', in class '{targetType}'") { - this.targetType = targetType; - this.propertyName = propertyName; - accessorType = fieldName; + TargetType = targetType; + PropertyName = propertyName; + AccessorType = fieldName; } /// @@ -65,23 +59,41 @@ public PropertyNotFoundException(string propertyName, string fieldName, System.T /// /// The that contains contextual information about the source or destination. /// - protected PropertyNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) + protected PropertyNotFoundException(SerializationInfo info, StreamingContext context) + : base(info, context) { + foreach (var entry in info) + { + if (entry.Name == "PropertyName") + { + PropertyName = entry.Value?.ToString(); + } + else if (entry.Name == "AccessorType") + { + AccessorType = entry.Value?.ToString(); + } + else if (entry.Name == "TargetType") + { + var typeName = entry.Value?.ToString(); + if (!string.IsNullOrEmpty(typeName)) + TargetType = System.Type.GetType(typeName, true); + } + } } - public System.Type TargetType + [SecurityCritical] + public override void GetObjectData(SerializationInfo info, StreamingContext context) { - get { return targetType; } + base.GetObjectData(info, context); + info.AddValue("TargetType", TargetType?.AssemblyQualifiedName); + info.AddValue("PropertyName", PropertyName); + info.AddValue("AccessorType", AccessorType); } - public string PropertyName - { - get { return propertyName; } - } + public System.Type TargetType { get; } - public string AccessorType - { - get { return accessorType; } - } + public string PropertyName { get; } + + public string AccessorType { get; } } -} \ No newline at end of file +} diff --git a/src/NHibernate/SchemaValidationException.cs b/src/NHibernate/SchemaValidationException.cs index f679fa570e7..2d654a07583 100644 --- a/src/NHibernate/SchemaValidationException.cs +++ b/src/NHibernate/SchemaValidationException.cs @@ -1,10 +1,12 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Runtime.Serialization; using System.Security; namespace NHibernate { + [Serializable] public class SchemaValidationException : HibernateException { public ReadOnlyCollection ValidationErrors { get; } diff --git a/src/NHibernate/Util/TypeNameParser.cs b/src/NHibernate/Util/TypeNameParser.cs index 958bbf2d39a..aca2a600549 100644 --- a/src/NHibernate/Util/TypeNameParser.cs +++ b/src/NHibernate/Util/TypeNameParser.cs @@ -1,14 +1,20 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.Serialization; using System.Text; using System.Text.RegularExpressions; namespace NHibernate.Util { + [Serializable] public class ParserException : Exception { public ParserException(string message) : base(message) { } + + protected ParserException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } } public class TypeNameParser @@ -232,4 +238,4 @@ private static bool NeedDefaultAssembly(string typeFullName) return NeedDefaultNamespaceOrDefaultAssembly(typeFullName) && typeFullName.IndexOf(',') < 0; } } -} \ No newline at end of file +}