Convert object to csharp object initialization code

Below is a class that will convert any object into c# code that would statically initialize that object.

Why is this cool? - Suppose you need to write unit tests for a web service with quite complex structures (I'm looking at you swift/iso22000). Manually coding data into tests could take quite a while but if you already have some examples from logged requests or sample XML files you could use something like the below class to emit c# object initialization code and BAM your template code is written.

public static class ObjectInitializationSerializer
{
    private static string GetCSharpString(object o)
    {
        if (o is bool)
        {
            return $"{o.ToString().ToLower()}";
        }
        if (o is string)
        {
            return $"\"{o}\"";
        }
        if (o is int)
        {
            return $"{o}";
        }
        if (o is decimal)
        {
            return $"{o}m";
        }
        if (o is DateTime)
        {
            return $"DateTime.Parse(\"{o}\")";
        }
        if (o is Enum)
        {
            return $"{o.GetType().FullName}.{o}";
        }
        if (o is IEnumerable)
        {
            return $"new {GetClassName(o)} \r\n{{\r\n{GetItems((IEnumerable)o)}}}";
        }

        return CreateObject(o).ToString();
    }

    private static string GetItems(IEnumerable items)
    {
        return items.Cast().Aggregate(string.Empty, (current, item) => current + $"{GetCSharpString(item)},\r\n");
    }

    private static StringBuilder CreateObject(object o)
    {
        var builder = new StringBuilder();
        builder.Append($"new {GetClassName(o)} \r\n{{\r\n");

        foreach (var property in o.GetType().GetProperties())
        {
            var value = property.GetValue(o);
            if (value != null)
            {
                builder.Append($"{property.Name} = {GetCSharpString(value)},\r\n");
            }
        }

        builder.Append("}");
        return builder;
    }

    private static string GetClassName(object o)
    {
        var type = o.GetType();

        if (type.IsGenericType)
        {
            var arg = type.GetGenericArguments().First().Name;
            return type.Name.Replace("`1", $"<{arg}>");
        }

        return type.Name;
    }

    public static string Serialize(object o)
    {
        return $"var newObject = {CreateObject(o)};";
    }
}

How to write confusing angular - Changing the default angular delimiter.

I just spent a wasted hour trying to figure out why an angular app written by one of my employers contracts was behaving so strangely. Apparently in angular you can override the default {{}} delimiter like so:

var myApp = angular.module('myApp', [], function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
});
You would now define a template{{ var }} as [[ var ]]

Check the documentation on the $interpolate service here: http://docs.angularjs.org/api/ng.$interpolate
I really can't think of a reason why you would want to do this offhand - unless you are trying to be a troll Javascript hipster!

Great trap for young players.

WCFExtras XML comment doesn't work?

WCFExtra's is an awesome addition to any WCF project which enables you to include your code comments as documentation in the generated wsdl.

The documentation is pretty ordinary though. To make the XMLComments work first we need to follow the instructions here: https://wcfextrasplus.codeplex.com/wikipage?title=XML%20Comments&referringTitle=Documentation

Next, you need to enable XML document generation in your WCF project properties as follows: 
  1. Right click on your project and select properties
  2. Go to the build tab
  3. Tick XML documentation file. Leave the generated filename as is.

Repeat this process for any other references projects that are used in your service contract.

Now we should see wsdl:documentation tag appeared in our WSDL

How to ignore non-trival differences in files with Beyond Compare

If you want to ignore something that can't be handled by a replacements because the to-state isn't easily defined such as mismatching page numbers you need to use a regular expression grammer to ignore certain strings.
In my case I needed to ignore Page numbers in a document footer
Here is how to do it:
  1. Load your comparison in Beyond Compare
  2. Click the Rules toolbar button (referee icon).
  3. On the Importance tab, click Edit Grammar.
  4. Click New.
  5. In Text matching enter your regex e.g. "Page\s\d{1,3}\sof\s\d{1,3}"
  6. Tick Regular Expression
  7. Click OK.
  8. Click OK.
  9. Un-check the element in the Grammar elements list to make it unimportant.
  10. Click OK
Your grammar should now be marked as unimportant (blue text)

How to open an excel document in new instance

Question: How do you force excel to open a document in a new instance

Answer:
C:\Users\[*your user name*]\AppData\Roaming\Microsoft\Windows\SendTo

Create a shortcut from the excel executable. For me (Office 2010) this is located here C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE, Call the shortcut “Excel”.

Now you can open the Excel-files by right clicking on your document > send to > Excel

WCF .svc handler mapping not working in IIS

Problem:
When trying to access a WCF .svc endpoint hosted by IIS I receive the following error "The resource you are looking for does not have a handler associated with it."

Solution:
1.    Run Server Manager (on task bar and start menu)
2.    Choose the server to administer (probably local server)
3.    Scroll down to "Roles and Features" section.
4.    Choose "Add Role or Feature" from Tasks drop down
5.    On "Add Role or Feature Wizard" dialog, click down to "Features" in list of pages on the left.
6.    Expand ".Net 3.5" or ".Net 4.5", depending on what you have installed. (you can go back up to "roles" screen to add if you don't have.
7.    Under "WCF Services", check the box for "HTTP-Activation". You can also add non-http types if you know you need them (tcp, named pipes, etc).
8.    Click "Install" Button.

Works on Windows Server 2008-2012

How do I add an active directory security group to a sql server sysadminrole

Question: How do I add an active directory security group to a sql server role such as sysadmin role

Answer:
EXEC master..sp_addsrvrolemember @loginame = N'\', @rolename = N'sysadmin'
GO

How do I check the Oracle version using SQL

Question: How do I check the Oracle version using SQL?

Answer: Version information is stored in a table called v$version. In this table you can find version information on Oracle, PL/SQL, etc.
To retrieve the version information for Oracle, you execute the following SQL statement:

select * from v$version
where banner like 'Oracle%';

It should return something like this:

Banner
--------------------------------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.1.0 - 64bit Production

How To Solve "Saving Changes Is Not Permitted" On SQL Server 2008 Management Studio

Problem: Saving changes is not permitted when doing alter table (table structure is changed):
1.       Change data type on existing columns
2.       Or change allow nulls on existing columns

Solution: To allow you to save changes after you 
alter table disable the prevent changes setting:
 
1.       Open Microsoft SQL Server Management Studio 2008
2.       Click Tools menu options, then click Options 
3.       Select Designers 
4.       Uncheck "prevent saving changes that require table re-creation" option
5.       Click OK
6.       Try to alter your table
7.       Your changes will performed as desired  

Cannot create an instance of OLE DB provider "OraOLEDB.Oracle" for linked server

Problem: I receive this error.
Msg 7302, Level 16, State 1, Line 1
Cannot create an instance of OLE DB provider "OraOLEDB.Oracle" for linked server "xxxx.WORLD".


Solution
Go to the the OraOLEDB.Oracle provider options and tick "Allow inprocess"