using System; using System.Diagnostics; using System.Runtime.Serialization; using System.Security; using System.Security.Permissions; using JetBrains.Annotations; namespace Microsoft.Win32.TaskScheduler { /// /// Abstract class for throwing a method specific exception. /// [DebuggerStepThrough, Serializable] [PublicAPI] public abstract class TSNotSupportedException : Exception { /// Defines the minimum supported version for the action not allowed by this exception. protected readonly TaskCompatibility min; private readonly string myMessage; /// /// Initializes a new instance of the class. /// /// The serialization information. /// The streaming context. protected TSNotSupportedException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext) { try { min = (TaskCompatibility)serializationInfo.GetValue("min", typeof(TaskCompatibility)); } catch { min = TaskCompatibility.V1; } } internal TSNotSupportedException(TaskCompatibility minComp) { min = minComp; var stackTrace = new StackTrace(); var stackFrame = stackTrace.GetFrame(2); var methodBase = stackFrame.GetMethod(); myMessage = $"{methodBase.DeclaringType?.Name}.{methodBase.Name} is not supported on {LibName}"; } internal TSNotSupportedException(string message, TaskCompatibility minComp) { myMessage = message; min = minComp; } /// /// Gets a message that describes the current exception. /// public override string Message => myMessage; /// /// Gets the minimum supported TaskScheduler version required for this method or property. /// public TaskCompatibility MinimumSupportedVersion => min; internal abstract string LibName { get; } /// /// Gets the object data. /// /// The information. /// The context. [SecurityCritical, SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) throw new ArgumentNullException(nameof(info)); info.AddValue("min", min); base.GetObjectData(info, context); } } /// /// Thrown when the calling method is not supported by Task Scheduler 1.0. /// [DebuggerStepThrough, Serializable] public class NotV1SupportedException : TSNotSupportedException { /// /// Initializes a new instance of the class. /// /// The serialization information. /// The streaming context. protected NotV1SupportedException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext) { } internal NotV1SupportedException() : base(TaskCompatibility.V2) { } /// /// Initializes a new instance of the class. /// /// The message. public NotV1SupportedException(string message) : base(message, TaskCompatibility.V2) { } internal override string LibName => "Task Scheduler 1.0"; } /// /// Thrown when the calling method is not supported by Task Scheduler 2.0. /// [DebuggerStepThrough, Serializable] public class NotV2SupportedException : TSNotSupportedException { /// /// Initializes a new instance of the class. /// /// The serialization information. /// The streaming context. protected NotV2SupportedException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext) { } internal NotV2SupportedException() : base(TaskCompatibility.V1) { } internal NotV2SupportedException(string message) : base(message, TaskCompatibility.V1) { } internal override string LibName => "Task Scheduler 2.0 (1.2)"; } /// /// Thrown when the calling method is not supported by Task Scheduler versions prior to the one specified. /// [DebuggerStepThrough, Serializable] public class NotSupportedPriorToException : TSNotSupportedException { /// /// Initializes a new instance of the class. /// /// The serialization information. /// The streaming context. protected NotSupportedPriorToException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext) { } internal NotSupportedPriorToException(TaskCompatibility supportedVersion) : base(supportedVersion) { } internal override string LibName => $"Task Scheduler versions prior to 2.{((int)min) - 2} (1.{(int)min})"; } }