c# json serialization

Two useful methods when comes to work with json format c#.

        public static T Deserialise(string json)
        {
            T obj = Activator.CreateInstance();
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T)serializer.ReadObject(ms); 
                return obj;
            }
        }

        public static string Serialize(T obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                return Encoding.Default.GetString(ms.ToArray());
            }
        }

How to build a project using C# 4.0?

If you want to build a project using C# in .NET 4.0 then you should use two important namespaces:

using Microsoft.Build.Evaluation;
using Microsoft.Build.Logging;

After you get an instance to the global project collection, you need to load you project.

var projectCollection = ProjectCollection.GlobalProjectCollection;
var project = projectCollection.LoadProject(@"d:\Path\To\Project\My.Web.csproj");

Setting the logger is an important step too.

var fileLogger = new FileLogger();
fileLogger.Parameters = @"logfile=" + logfilePath;
projectCollection.RegisterLogger(fileLogger);

Start the build process and grab the result. After that make sure you unregister the logger.

bool result = project.Build();
projectCollection.UnregisterAllLoggers();
return result;