Generic .NET Extensions and Helpers

Assembly
WebExtras.dll
Namespace
WebExtras.Core

Object Extensions

WebExtras provides a general purpose object cloner which performs a deep clone of the given object. However, in order for the cloning to work, all objects (including any child object of the class) to be cloned must be marked as serializable using the [Serializable] decorator attribute.

Markup


  SomeClass object1 = new SomeClass();
  SomeClass object2 = object1.DeepClone();
  

Enum Extensions

WebExtras provides two enum extension methods viz., ToTitleCase() and GetStringValue()

Consider the following C# enumeration
ToTitleCase() as the method name suggests will convert the enum value to it's titlecase representation based on the current system culture

  // Enum definition
  public enum TestEnum
  {
    [StringValue("My great Enum")]
    enumvalue = 0,

    [StringValue(typeof(MyCustomStringValueDecider))]
    customEnumValue = 1
  }
    

  // Invocation of ToTitleCase
  TestEnum.enumvalue.ToTitleCase()

  // Output
  Enumvalue
    
GetStringValue() simply retrieves the string value decorated either using a static text or by using a IStringValueDecider implementation of the StringValue attribute. This is especially useful if you want an enum value to have an integer representation as well as a string representation.

  // Invocation of GetStringValue
  TestEnum.enumvalue.GetStringValue()

  // Output
  My great Enum
    

  // This is how MyCustomStringValueDecider class looks
  class MyCustomStringValueDecider : IStringValueDecider
  {
    public string Decide()
    {
      // Let us say we evaluated some condition here
// and our result was "my decided value"
return "my decided value"; } } // Invocation of GetStringValue on a value that has a
// custom string value decider
TestEnum.customEnumValue.GetStringValue() // Output my decided value

String Extensions

WebExtras provides two string extension methods viz., ToTitleCase() and ContainsIgnoreCase()

ToTitleCase() as the method name suggests will convert the string value to it's titlecase representation based on the current system culture
ContainsIgnoreCase() simply compares the current string with a given string and checks whether the current string contains the given string irrespective of case
Remove() method removes all occurences of the given string from it's parent string. This extension simply uses the Replace() method internally but making use of Remove() is semantically clearer.

  // String definition
  string str = "basic"

  // Invocation of ToTitleCase
  str.ToTitleCase()

  // Output
  Basic
    

  // String definition
  string str = "basic"

  // Invocation of ContainsIgnoreCase
  str.ContainsIgnoreCase("BAS")

  // Output
  true
    

  // String definition 
  string str = "basically basic"

  // Invocation of Remove 
  str.Remove("bas")
            
  // Output 
  ically ic
          

Double Extensions

WebExtras provides ability to convert JavaScript ticks to a .NET DateTime object by using the ToDateTime() extension method available to all double values


  double ticks = 1356998400000;
  DateTime dt = ticks.ToDateTime();       // Parsed date time is 01/01/2013 12:00:00 AM
  

DateTime Extensions

As with doubles, WebExtras also provides ability to convert a .NET DateTime object to it's equivalent JavaScript timestamp by using the ToJavaScriptDate() extension method available to all DateTime values


  DateTime dt = DateTime.Parse("2013-01-01 00:00:00");
  double ticks = dt.ToJavaScriptDate();       // Parsed JavaScript ticks are 1356998400000
  

JsFunc utility class

In order to create and return custom JavaScript functions for the DataTables and Flot hooks, WebExtras provides the WebExtras.Core.JsFunc class.

Markup


JsFunc foo = new JsFunc
{
  Name = "foo",
  ParameterNames = new string[] { "myParam1", "myParam2" },
  Body = "alert(myParam1 + '...' + myParam2);",
  OnDocumentReady = true
};
  

And the serialised output will be


// You MUST use the Json.NET serialiser in order to serialise properly
$(document).ready(function() {
  function foo(myParam1, myParam2) {
    alert(myParam1 + '...' + myParam2);
  };
});