Categories
How To

How to get the Description Attribute in C#

		[CanBeNull]
		public static String? GetDescription<T>( [CanBeNull] this T e ) where T : IConvertible {
			if ( e is not Enum ) {
				return default( String? );
			}

			var type = e.GetType();

			foreach ( Int32 val in Enum.GetValues( type ) ) {
				if ( val != e.ToInt32( CultureInfo.InvariantCulture ) ) {
					continue;
				}

				var ename = type.GetEnumName( val );

				if ( ename is null ) {
					continue;
				}

				var memInfo = type.GetMember( ename );

				if ( memInfo[0].GetCustomAttributes( typeof( DescriptionAttribute ), false ).FirstOrDefault() is DescriptionAttribute descriptionAttribute ) {
					return descriptionAttribute.Description;
				}
			}

			return default( String? );
		}

Categories
How To

How to check the return Status in C#

	public static class StatusExtensions {

		static StatusExtensions() {
			if ( Status.Good.IsBad() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}

			if ( Status.Failure.IsGood() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}

			if ( Status.Success.IsBad() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}

			if ( !Status.Unknown.IsUnknown() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}
		}

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean Failed( this Status status ) => status <= Status.Failure;

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean IsBad( this Status status ) => status.Failed();

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean IsUnknown( this Status status ) => status == Status.Unknown || !status.IsBad() && !status.IsGood();

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean Succeeded( this Status status ) => status >= Status.Success;

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean IsGood( this Status status ) => status.Succeeded();

		[Pure]
		[NotNull]
		public static String Symbol( this Status status ) => status.GetDescription() ?? Symbols.Null;

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Status ToStatus( this Boolean status ) => status ? Status.Success : Status.Failure;

	}

Categories
How To

How to Return Status in C#

I use the design pattern of returning a Status. Example:

	public enum Status : Int16 {

		[Description( Symbols.SkullAndCrossbones )]
		Fatal = Exception - 1,

		[Description( Symbols.Exception )]
		Exception = Error - 1,

		[Description( Symbols.Error )]
		Error = Warning - 1,

		[Description( Symbols.Warning )]
		Warning = Skip - 1,

		[Description( Symbols.Fail )]
		Skip = Timeout - 1,

			[Description( Symbols.Timeout )]
			Timeout = Stop - 1,

				[Description( Symbols.StopSign )]
				Stop = Halt - 1,

					[Description( Symbols.StopSign )]
					Halt = Negative - 1,

						[Description( Symbols.FailBig )]
						Negative = No - 1,

							[Description( Symbols.Fail )]
							No = Bad - 1,

								[Description( Symbols.Fail )]
								Bad = Failure - 1,

									[Description( Symbols.BlackStar )]
									Failure = -1,

										[Description( Symbols.Unknown )]
										Unknown = 0,

									[Description( Symbols.WhiteStar )]
									Success = 1,

									[Description( Symbols.WhiteStar )]
									Okay = Success,

								[Description( Symbols.CheckMark )]
								Good = Success + 1,

							[Description( Symbols.CheckMark )]
							Yes = Good + 1,

						[Description( Symbols.CheckMark )]
						Positive = Yes + 1,

					[Description( Symbols.CheckMark )]
					Continue = Positive + 1,

				[Description( Symbols.CheckMark )]
				Go = Continue + 1,

			[Description( Symbols.CheckMark )]
			Proceed = Go + 1,

		[Description( Symbols.CheckMark )]
		Advance = Proceed + 1,

		[Description( Symbols.CheckMark )]
		Flawless = Advance + 1
	}

Categories
How To

How to return null if a String is null or Empty in C#

        /// <summary>Returns null if <paramref name="self" /> is <see cref="String.IsNullOrEmpty" />.</summary>
        /// <param name="self"></param>
        /// <returns></returns>
        [CanBeNull]
        [DebuggerStepThrough]
        [Pure]
        public static String? NullIfEmpty( [CanBeNull] this String? self ) => String.IsNullOrEmpty( self ) ? null : self;


Categories
How To

How to Trim whitespace from a String in C#

        /// <summary>Trim the ToString() of the object; returning null if null, empty, or whitespace.</summary>
        /// <param name="self"></param>
        /// <returns></returns>
        [DebuggerStepThrough]
        [CanBeNull]
        [Pure]
        public static String? Trimmed<T>( [CanBeNull] this T self ) =>
            self switch {
                null => default( String? ),
                String s => s.Trim().NullIfEmpty(),
                var _ => self.ToString()?.Trim().NullIfEmpty()
            };

Categories
How To

How to convert a Guid to BigInteger in C#

        [DebuggerStepThrough]
        [Pure]
        public static BigInteger ToBigInteger( this Guid self ) => new( self.ToByteArray() );

Categories
Protiguous Quote

Get Angry

Get angry.. Do something positive about it.
Just don’t stay mad.

Protiguous, 2021

Categories
Anything

Don’t Rush Me

Good riddance, you vile racist cigar smoking scumbag.

Categories
SQL Server

Microsoft SQL Server Updates and Patches

https://sqlserverbuilds.blogspot.com/

Categories
truth

Cult Warning Signs

  • The Chosen One is always right.
  • Criticism of the Chosen One is wrong and shameful.
  • Anything the Chosen One does is justified; ignore the consequences.
  • The Chosen One is the only source of Truth, everybody else is lying.
  • Anyone not loyal to the Chosen One is the enemy.