Tag Archives: ExtensionMethod

Cinchoo – Queued Execution Service

ChoQueuedExecutionService

In this article, we will go over the one of service provided by Cinchoo framework, ChoQueuedExecutionService. This service used to execute series of methods in orderly fashion. Each operation pushed to this service will be executed in first come first serve basis. Also we can instruct to run each operation with Timeout (ms), Number of retries (in case of error while running), sleep between retries parameters.

Either you can use Global QueuedExectionService (application level service) or instantiate new one locally and use them.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

Using Global QueuedExecutionService:

Below sample, inject a delegate to Global execution service and wait for the call to complete.

static void Main(string[] args)
{
    IChoAsyncResult result = ChoQueuedExecutionService.Global.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Creating and using local QueuedExecutionService:

In below sample, we create a local QueuedExecutionService and injecting method to execute.

static void Main(string[] args)
{
    ChoQueuedExecutionService service = new ChoQueuedExecutionService("TestQService", false);

    IChoAsyncResult result = service.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Try for yourself!

Advertisements

Cinchoo – .NET TypeConverters

List of TypeConverters

Below are the list of .NET TypeConverters available, these can be used in Cinchoo framework in various places i.e ChoObject.ToString(), ConfigurationManagement etc

Microsoft.VisualBasic.Activities.XamlIntegration.VisualBasicSettingsConverter
Microsoft.VisualBasic.ApplicationServices.BuiltInRoleConverter
System.Activities.XamlIntegration.TypeConverterBase
System.ComponentModel.BaseNumberConverter
System.ComponentModel.BooleanConverter
System.ComponentModel.CharConverter
System.ComponentModel.CollectionConverter
System.ComponentModel.CultureInfoConverter
System.ComponentModel.DateTimeConverter
System.ComponentModel.DateTimeOffsetConverter
System.ComponentModel.EnumConverter
System.ComponentModel.ExpandableObjectConverter
System.ComponentModel.GuidConverter
System.ComponentModel.MultilineStringConverter
System.ComponentModel.NullableConverter
System.ComponentModel.ReferenceConverter
System.ComponentModel.StringConverter
System.ComponentModel.TimeSpanConverter
System.ComponentModel.TypeListConverter
System.Configuration.ConfigurationConverterBase
System.Diagnostics.Design.LogConverter
System.Drawing.ColorConverter
System.Drawing.FontConverter
System.Drawing.FontConverter.FontNameConverter
System.Drawing.ImageConverter
System.Drawing.ImageFormatConverter
System.Drawing.PointConverter
System.Drawing.RectangleConverter
System.Drawing.SizeConverter
System.Drawing.SizeFConverter
System.Resources.ResXFileRef.Converter
System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicyTypeConverter
System.ServiceModel.Discovery.Configuration.DiscoveryVersionConverter
System.ServiceModel.XamlIntegration.EndpointIdentityConverter
System.ServiceModel.XamlIntegration.ServiceXNameTypeConverter
System.ServiceModel.XamlIntegration.XPathMessageContextTypeConverter
System.UriTypeConverter
System.Web.Configuration.LowerCaseStringConverter
System.Web.UI.Design.DataBindingCollectionConverter
System.Web.UI.Design.DataColumnSelectionConverter
System.Web.UI.Design.DataFieldConverter
System.Web.UI.Design.DataMemberConverter
System.Web.UI.Design.DataSourceConverter
System.Web.UI.Design.DataSourceViewSchemaConverter
System.Web.UI.Design.ExpressionsCollectionConverter
System.Web.UI.Design.MobileControls.Converters.DataFieldConverter
System.Web.UI.Design.MobileControls.Converters.DataMemberConverter
System.Web.UI.Design.SkinIDTypeConverter
System.Web.UI.Design.WebControls.DataSourceIDConverter
System.Web.UI.WebControls.FontNamesConverter
System.Web.UI.WebControls.FontUnitConverter
System.Web.UI.WebControls.StringArrayConverter
System.Web.UI.WebControls.UnitConverter
System.Windows.Controls.DataGridLengthConverter
System.Windows.CornerRadiusConverter
System.Windows.CultureInfoIetfLanguageTagConverter
System.Windows.DeferrableContentConverter
System.Windows.DialogResultConverter
System.Windows.DurationConverter
System.Windows.DynamicResourceExtensionConverter
System.Windows.ExpressionConverter
System.Windows.FigureLengthConverter
System.Windows.FontSizeConverter
System.Windows.FontStretchConverter
System.Windows.FontStyleConverter
System.Windows.FontWeightConverter
System.Windows.Forms.AxHost.StateConverter
System.Windows.Forms.CursorConverter
System.Windows.Forms.DataGridPreferredColumnWidthTypeConverter
System.Windows.Forms.DataGridViewCellStyleConverter
System.Windows.Forms.KeysConverter
System.Windows.Forms.Layout.TableLayoutSettingsTypeConverter
System.Windows.Forms.LinkArea.LinkAreaConverter
System.Windows.Forms.LinkConverter
System.Windows.Forms.ListBindingConverter
System.Windows.Forms.OpacityConverter
System.Windows.Forms.PaddingConverter
System.Windows.Forms.ScrollableControl.DockPaddingEdgesConverter
System.Windows.Forms.SelectionRangeConverter
System.Windows.Forms.TreeNodeConverter
System.Windows.GridLengthConverter
System.Windows.Input.CommandConverter
System.Windows.Input.CursorConverter
System.Windows.Input.InputScopeConverter
System.Windows.Input.InputScopeNameConverter
System.Windows.Input.KeyConverter
System.Windows.Input.KeyGestureConverter
System.Windows.Input.ModifierKeysConverter
System.Windows.Input.MouseActionConverter
System.Windows.Input.MouseGestureConverter
System.Windows.Int32RectConverter
System.Windows.KeySplineConverter
System.Windows.KeyTimeConverter
System.Windows.LengthConverter
System.Windows.Markup.DependencyPropertyConverter
System.Windows.Markup.EventSetterHandlerConverter
System.Windows.Markup.NameReferenceConverter
System.Windows.Markup.RoutedEventConverter
System.Windows.Markup.SetterTriggerConditionValueConverter
System.Windows.Markup.TemplateKeyConverter
System.Windows.Markup.XmlLanguageConverter
System.Windows.Media.Animation.RepeatBehaviorConverter
System.Windows.Media.BrushConverter
System.Windows.Media.CacheModeConverter
System.Windows.Media.ColorConverter
System.Windows.Media.Converters.BaseIListConverter
System.Windows.Media.DoubleCollectionConverter
System.Windows.Media.FontFamilyConverter
System.Windows.Media.GeometryConverter
System.Windows.Media.ImageSourceConverter
System.Windows.Media.Int32CollectionConverter
System.Windows.Media.MatrixConverter
System.Windows.Media.Media3D.Matrix3DConverter
System.Windows.Media.Media3D.Point3DCollectionConverter
System.Windows.Media.Media3D.Point3DConverter
System.Windows.Media.Media3D.Point4DConverter
System.Windows.Media.Media3D.QuaternionConverter
System.Windows.Media.Media3D.Rect3DConverter
System.Windows.Media.Media3D.Size3DConverter
System.Windows.Media.Media3D.Vector3DCollectionConverter
System.Windows.Media.Media3D.Vector3DConverter
System.Windows.Media.PathFigureCollectionConverter
System.Windows.Media.PixelFormatConverter
System.Windows.Media.PointCollectionConverter
System.Windows.Media.RequestCachePolicyConverter
System.Windows.Media.TransformConverter
System.Windows.Media.VectorCollectionConverter
System.Windows.PointConverter
System.Windows.PropertyPathConverter
System.Windows.RectConverter
System.Windows.SizeConverter
System.Windows.StrokeCollectionConverter
System.Windows.TemplateBindingExpressionConverter
System.Windows.TemplateBindingExtensionConverter
System.Windows.TextDecorationCollectionConverter
System.Windows.ThicknessConverter
System.Windows.VectorConverter
System.Workflow.ComponentModel.Design.ActivityBindTypeConverter
System.Xaml.Schema.XamlTypeTypeConverter


Cinchoo – String.Unindent(), Extension Method

In this section, I’ll talk about Unindent extension method. This method used to find and remove the padding characters from input text. Please see the below samples on how to use it.

1. Add reference to Cinchoo.Core.ExtensionMethods.dll assembly

2. Namespace System

Sample:

static void Main(string[] args)
{
    string msg = "\tHello World!{0}\tWelcome to Cinchoo.com".FormatString(Environment.NewLine);

    Console.WriteLine("Before Unindent:");
    Console.WriteLine(msg);
    Console.WriteLine();

    Console.WriteLine("After Unindent:");
    Console.WriteLine(msg.Unindent());
}

When you run the above code, the output will be

Before Unindent:
        Hello World!
        Welcome to Cinchoo.com

After Unindent:
Hello World!
Welcome to Cinchoo.com
Press any key to continue . . .

Unindent() method has several overloads, they are

//Unindent with 1 tab char
Unindent();
//Unindent with 'totalWidth' number of tab chars
Unindent(int totalWidth);
//Unindent with 'totalWidth' number of paddingChars
Unindent(int totalWidth, char paddingChar);

PS: totalWidth should be positive. In case if you pass negative value, this routine will invoke Indent() method to add any leading pad characters from each line of input text.

Happy coding!!!

Cinchoo – Enum.ToDescription(), Extension Method

In this section, I’ll talk about ToDescription<T> extension method. This method used get enum description from enum value.

1. Add reference to Cinchoo.Core.ExtensionMethods.dll assembly

2. Namespace System

Sample:

public enum Color
{
    [Description("Red Color")]
    Red,
    [Description("Green Color")]
    Green,
    [Description("Yellow Color")]
    Yellow
}

static void Main(string[] args)
{
    Console.Write("Description of 'Color.Green' enum value is: ");
    Console.WriteLine(Color.Green.ToDescription());
}

When you run the above code, the output will be

Description of 'Color.Green' enum value is: Green Color
Press any key to continue . . .

Happy coding!!!

Cinchoo – String.ToEnum(), Extension Method

In this section, I’ll talk about ToEnum<T> extension method. This method used get enum value from enum description.

1. Add reference to Cinchoo.Core.ExtensionMethods.dll assembly

2. Namespace System

Sample:

public enum Color
{
    [Description("Red Color")]
    Red,
    [Description("Green Color")]
    Green,
    [Description("Yellow Color")]
    Yellow
}

static void Main(string[] args)
{
    Console.Write("Enum value for 'RED Color' is: ");
    Console.WriteLine("RED Color".ToEnum<Color>());
}

When you run the above code, the output will be

Enum value for 'RED Color' is: Red
Press any key to continue . . .

Happy coding!!!

Cinchoo – String.Indent(), Extension Method

In this section, I’ll talk about Indent extension method. This method used to indent a string with the padding characters. Please see the below samples on how to use it.

1. Add reference to Cinchoo.Core.ExtensionMethods.dll assembly

2. Namespace System

Sample:

static void Main(string[] args)
{
    string msg = "Hello World!{0}Welcome to Cinchoo.com".FormatString(Environment.NewLine);

    Console.WriteLine("Before Indent:");
    Console.WriteLine(msg);
    Console.WriteLine();

    Console.WriteLine("After Indent:");
    Console.WriteLine(msg.Indent());
}

When you run the above code, the output will be

Before Indent:
Hello World!
Welcome to Cinchoo.com

After Indent:
        Hello World!
        Welcome to Cinchoo.com

Press any key to continue . . .

Indent() method has several overloads, they are

//Indent with 1 tab char
Indent();
//Indent with 'totalWidth' number of tab chars
Indent(int totalWidth);
//Indent with 'totalWidth' number of paddingChars
Indent(int totalWidth, char paddingChar);

PS: totalWidth should be positive. In case if you pass negative value, this routine will invoke Unindent() method to remove any leading pad characters from each line of input text.

Happy coding!!!