public static void Sanitize(Node node) { Node node = new Document(); new Sanitizer().Cleanup(node); // void Cleanup(Node node) } class Sanitizer { public void Cleanup(Node node) { } public void Cleanup(Element element) { } public void Cleanup(Attribute attribute) { } public void Cleanup(Document document) { } }
class Node { } class Attribute : Node { } class Document : Node { } class Element : Node { } class Text : Node { } class HtmlElement : Element { } class HtmlDocument : Document { }
void Cleanup(Node node)
method will be selected. This problem can be solved by OOP-approach, or use type conversion. public static void Sanitize(Node node) { var sanitizer = new Sanitizer(); var document = node as Document; if (document != null) { sanitizer.Cleanup(document); } var element = node as Element; if (element != null) { sanitizer.Cleanup(element); } /* * */ { // - sanitizer.Cleanup(node); } }
public static void Sanitize(Node node) { var sanitizer = new Sanitizer(); switch (node.NodeType) { case NodeType.Node: sanitizer.Cleanup(node); break; case NodeType.Element: sanitizer.Cleanup((Element)node); break; case NodeType.Document: sanitizer.Cleanup((Document)node); break; case NodeType.Text: sanitizer.Cleanup((Text)node); break; case NodeType.Attribute: sanitizer.Cleanup((Attribute)node); break; default: throw new ArgumentOutOfRangeException(); } } enum NodeType { Node, Element, Document, Text, Attribute } abstract class Node { public abstract NodeType NodeType { get; } } class Attribute : Node { public override NodeType NodeType { get { return NodeType.Attribute; } } } class Document : Node { public override NodeType NodeType { get { return NodeType.Document; } } } class Element : Node { public override NodeType NodeType { get { return NodeType.Element; } } } class Text : Node { public override NodeType NodeType { get { return NodeType.Text; } } }
class Program { interface ICollidable { void CollideWith(ICollidable other); } class Asteroid : ICollidable { public void CollideWith(Asteroid other) { Console.WriteLine("Asteroid collides with Asteroid"); } public void CollideWith(Spaceship spaceship) { Console.WriteLine("Asteroid collides with Spaceship"); } public void CollideWith(ICollidable other) { other.CollideWith(this); } } class Spaceship : ICollidable { public void CollideWith(ICollidable other) { other.CollideWith(this); } public void CollideWith(Asteroid asteroid) { Console.WriteLine("Spaceship collides with Asteroid"); } public void CollideWith(Spaceship spaceship) { Console.WriteLine("Spaceship collides with Spaceship"); } } static void Main(string[] args) { var asteroid = new Asteroid(); var spaceship = new Spaceship(); asteroid.CollideWith(spaceship); asteroid.CollideWith(asteroid); } }
try { // code to throw exception } catch (InvalidCastException invalidCastException) { // log ex // rethrow if needed } catch (Exception e) { // throw new Exception with inner }
var policies = new List<ExceptionPolicyDefinition>(); var myTestExceptionPolicy = new List<ExceptionPolicyEntry> { { new ExceptionPolicyEntry(typeof (InvalidCastException), PostHandlingAction.NotifyRethrow, new IExceptionHandler[] {new LoggingExceptionHandler(...),}) }, { new ExceptionPolicyEntry(typeof (Exception), PostHandlingAction.NotifyRethrow, new IExceptionHandler[] {new ReplaceHandler(...)}) } }; policies.Add(new ExceptionPolicyDefinition("MyTestExceptionPolicy", myTestExceptionPolicy)); ExceptionManager manager = new ExceptionManager(policies); try { // code to throw exception } catch (Exception e) { manager.HandleException(e, "Exception Policy Name"); }
ExceptionManager manager = new ExceptionManager(policies); try { // code to throw exception } catch (Exception e) { manager.HandleException(e, "Exception Policy Name"); }
manager.HandleException(e, "Exception Policy Name")
private ExceptionPolicyEntry FindExceptionPolicyEntry(Type exceptionType) { ExceptionPolicyEntry policyEntry = null; while (exceptionType != typeof(object)) { policyEntry = this.GetPolicyEntry(exceptionType); if (policyEntry != null) { return policyEntry; } exceptionType = exceptionType.BaseType; } return policyEntry; }
public bool Handle(Exception exceptionToHandle) { if (exceptionToHandle == null) { throw new ArgumentNullException("exceptionToHandle"); } Guid handlingInstanceID = Guid.NewGuid(); Exception chainException = this.ExecuteHandlerChain(exceptionToHandle, handlingInstanceID); return this.RethrowRecommended(chainException, exceptionToHandle); }
private Exception ExecuteHandlerChain(Exception ex, Guid handlingInstanceID) { string name = string.Empty; try { foreach (IExceptionHandler handler in this.handlers) { name = handler.GetType().Name; ex = handler.HandleException(ex, handlingInstanceID); } } catch (Exception exception) { // rest of implementation } return ex; }
namespace Microsoft.Practices.EnterpriseLibrary.ExceptionHandling { public interface IExceptionHandler { Exception HandleException(Exception ex, Guid handlingInstanceID); } }
public interface IExceptionHandler { void HandleException<T>(T exception) where T : Exception; } public interface IExceptionHandler<T> where T : Exception { void Handle(T exception); }
public class FileSystemExceptionHandler : IExceptionHandler, IExceptionHandler<Exception>, IExceptionHandler<IOException>, IExceptionHandler<FileNotFoundException> { public void HandleException<T>(T exception) where T : Exception { var handler = this as IExceptionHandler<T>; if (handler != null) handler.Handle(exception); else this.Handle((dynamic) exception); } public void Handle(Exception exception) { OnFallback(exception); } protected virtual void OnFallback(Exception exception) { // rest of implementation Console.WriteLine("Fallback: {0}", exception.GetType().Name); } public void Handle(IOException exception) { // rest of implementation Console.WriteLine("IO spec"); } public void Handle(FileNotFoundException exception) { // rest of implementation Console.WriteLine("FileNotFoundException spec"); } }
IExceptionHandler defaultHandler = new FileSystemExceptionHandler(); defaultHandler.HandleException(new IOException()); // Handle(IOException) overload defaultHandler.HandleException(new DirectoryNotFoundException()); // Handle(IOException) overload defaultHandler.HandleException(new FileNotFoundException()); // Handle(FileNotFoundException) overload defaultHandler.HandleException(new FormatException()); // Handle(Exception) => OnFallback
public static void Sanitize(Node node) { new Sanitizer().Cleanup((dynamic)node); }
Source: https://habr.com/ru/post/283522/
All Articles