namespace System.Extensions { public static class ObjectExtensions { public static T Cast(this object obj) { return (T)obj; } public static T As(this object obj) where T : class { return obj as T; } public static Optional ToOptional(this T obj) { return Optional.FromValue(obj); } public static T ThrowIfNull([ValidatedNotNull] this T obj, string name) where T : class { if (obj is null) throw new ArgumentNullException(name); return obj; } [AttributeUsage(AttributeTargets.Parameter)] sealed class ValidatedNotNullAttribute : Attribute { } } }