Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Wednesday, 23 June 2010

Get browser information from Silverlight

Recently I needed to detect information about browser version within Silverlight. After googling around I found many posts that pointed to built-in class BrowserInformation, which can be accessed via System.Windows.Browser.HtmlPage.BrowserInformation.

So I should be happy and use its Name and BrowserVersion properties?

No, because BrowserVersion property returns something similar to HTML version instead of the real browser version. For example, on "Internet Explorer 7.0", it returns "4.0"

The "real" browser version can be extracted from the mysterious UserAgent property, just like this:

public static string BrowserName()
{
  string userAgent = HtmlPage.BrowserInformation.UserAgent;
  if (userAgent.IndexOf("MSIE 8.0") > 0)
  {
    return "Internet Explorer 8.0";
  }
  if (userAgent.IndexOf("MSIE 7.0") > 0)
  {
    return "Internet Explorer 7.0";
  }
  if (userAgent.IndexOf("MSIE 6.0") > 0)
  {
    return "Internet Explorer 6.0";
  }
  if (userAgent.IndexOf("MSIE 5.0") > 0)
  {
    return "Internet Explorer 5.0";
  }
  if (userAgent.IndexOf("Firefox") > 0)
  {
    return "Mozilla " + userAgent.Substring(userAgent.IndexOf("Firefox"), 100).Replace('/', ' ');
  }
  if (userAgent.IndexOf("Chrome") > 0)
  {
    return "Google " + userAgent.Substring(userAgent.IndexOf("Chrome"), userAgent.IndexOf("Safari") - userAgent.IndexOf("Chrome")).Replace('/', ' ');
  }
  if (userAgent.IndexOf("Safari") > 0)
  {
    return "Safari " + userAgent.Substring(userAgent.IndexOf("Version"), userAgent.IndexOf("Safari") - userAgent.IndexOf("Version")).Replace("Version/", String.Empty);
  }
  if (userAgent.IndexOf("Opera") > 0)
  {
    return userAgent.Substring(userAgent.IndexOf("Opera"), userAgent.IndexOf("(") - 2).Replace('/', ' ');
  }
  return "Unknown Browser";
}


* This source code was highlighted with Source Code Highlighter.

The code was successfully tested on IE and Chrome.

Thursday, 4 March 2010

Deep cloning object in C#/.NET

I was asked recently to write a generic method for deep cloning of a complex object. The object contains references to other objects, collections, which contain references to many other objects and collections. The method was supposed to work on both platforms: .NET 3.5 (or higher) and Silverlight.

While googling I found a good post listing different approaches for cloning
C# Object Clone Wars

Last technique described in that blog points to Copyable Framework - generic framework for copying/cloning any objects using method extensions

The framework, written by HÃ¥vard Stranden addresses most of my requirements, except one: it does not work on Silverlight.

I came up with a simple method that serializes the original object and deserializes it to a cloned copy of it. It can be used as an extension or by implementing IClonable interface:

public static class ObjectExtensions
{
  public static T Clone<T>(this object original)
  {
    T cloned;
    using (MemoryStream stream = new MemoryStream())
    {
      DataContractSerializer serializer = new DataContractSerializer(typeof(T));
      serializer.WriteObject(stream, original);
      stream.Position = 0;
      cloned = (T)serializer.ReadObject(stream);
    }
    return cloned;
  }
}


* This source code was highlighted with Source Code Highlighter.

Monday, 8 June 2009

Post-build assembly manipulation

We often think how to modify our code during writing, debugging, but rarely after assembly is built. Well, there are a few kinds of post-build manipulations we might want to perform on compiled assemblies:

Merge
This nice utility was developed by Mike Barnett from Microsoft Research and it enables merging multiple assemblies to a single one. This may be very useful, when one wants to deliver a single component or executable without exposing the internal structure of assemblies. It may be used as command-line utility (from post build action as well) and as a assembly reference and wrapped within another tool. It does not work on Silverlight assemblies, at least at the moment of writing these lines and I asked Mike to check this out.
http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx

Convert to Silverlight
This project developed by Suriel Bendahan enables converting assemblies compiled on server-side .NET to Silverlight compatible assemblies. It makes possible to reuse code written as "server-side" on Silverlight client. It might be useful for generic data structures, generic algorithms, input validations, sharing interfaces etc. The project is provided with it sources, so it can be modified/adopted for your needs.
http://www.codeproject.com/KB/silverlight/SLAssemblies.aspx

Compress
Silverlight assemblies are being downloaded to browser in a XAP file, which is a bunch of DLLs compressed using ZIP compression. This project developed by Rob Houweling uncompresses these assemblies and compresses them again with a higher compression rate. This is very useful when XAP size becomes an issue.
http://web-snippets.blogspot.com/2008/11/another-one-on-repacking-xaps-to-reduce.html

Obfuscate
Obfuscation is a process of changing the readability of the code without changing its functionality. The reason for doing this is preventing others from seeing implementation. Visual Studio comes with a Dotfuscator Community Edition and there are others providers of obfuscation utilities.