Generic Cloning Method in c#

20. November 2008

Since I found my drive again to create some code after I attended TechEd last week. Here's number 1 in what should become a nice code library.

 So you would like to clone an object do you? And you don't want to start writing code in each class or factory service for a certain object type do you?

Well here's one solution that goes for all object that are serializable, and we make all are classes serializable these days, don't we.

/// <summary>
/// Creates a cloned decoupled object from a serializable source object
/// </summary>
/// <typeparam name="T">The type of the object to clone</typeparam>
/// <param name="objectToClone">The object to clone</param>
/// <returns>A decoupled copy of the source object</returns>
public static T Clone<T>(T objectToClone)
{
  // Initialize to default null
  T clone = default(T);
  MemoryStream memoryStream = null;
  try
  {
    BinaryFormatter bf = new BinaryFormatter();
    memoryStream = new MemoryStream();
 
    // Serialize the object in memory
   bf.Serialize(memoryStream, objectToClone);
 
    // Make sure all is loaded in the stream
    memoryStream.Flush();
 
    // Reset the position
    memoryStream.Position = 0;
    // Decoupled copy of the original object
   clone = ((T) bf.Deserialize(memoryStream));
  }
  catch(Exception err)
  {
    LogService.LogError("Failed to clone an object.", err);
  }
  finally
  {
     if (memoryStream != null)
    {
      memoryStream.Close();
      memoryStream.Dispose();
    }
  }
  return clone;
}

Currently rated 2.0 by 2 people

  • Currently 2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

TechEd Barcelona 2008 - Day 1.1

10. November 2008

Yesterday I arrived in Barcelona to attend Microsoft TechEd 2008. After a pleasant flight (with a small, normal, big fuel leak - what's it going to be captain?) and long trip to the hotel we arrived at our hotel right next to the CCIB. We went out for some tapas at around twelve o'clock and I must say, here in Spain they know how to make them.

 So now I'm going to head of to register myself downstairs and grab some lunch later on.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development, Varia ,

ASP.NET 1.1 on IIS 7 using handler mappings

19. August 2008

The problem I was experiencing was that I could not set any custom handler mappings in IIS 7 because it would add a <system.webServer> tag to the web.config of my application. This would crash my asp.net 1.1 app because it doesn't understand the tag. So there I was wanting to use the new IIS 7 and not being able to set any custom handler mappings because they would be stored in the web.config of my application.

But there is a solution to this problem! You need to add a line to your machine.config in order for asp.net to ignore the above tag.

1. Go to the machine.config file for your .net 1.1 framework (C:\Windows\Microsoft.NET\Framework\v1.1.4322\CONFIG\machine.config)

2. Add the following line to it just above the </configSections> closing tag

    <section name="system.webServer" type="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

Et voila, there you go. The best of both worlds, if you need to debug 1.1 apps anyway.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Wanted: alive

20. June 2008

2 Senior C# .NET Web Developers

Mocht je zin hebben om mij en mijn collega's te vervoegen en in een ongelooflijk sociaal, dynamisch maar hard werkend team, dan kan je me altijd contacteren via het contact formulier. Hieronder volgt alvast een overzicht van je vereiste kunnen.

  • minstens 3/5 jaar relevante ervaring in C# ASP.NET web projecten
  • goede kennis .NET CLR 2.0/3.0/3.5
  • kennis van .NET CLR 1.1 migratie naar CLR 2.0 is pluspunt
  • goede kennis van Windows Systemen & IIS 6.0/7.0
  • goede kennis van SQL Server (2000/2005)
  • goede kennis van HTML/CSS/XML
  • goede kennis van Javascript
  • goede OO skills
  • kennis van Photoshop pluspunt
  • kennis van SEO pluspunt
  • andere relevante IT kennis (andere dev. talen, systemen, etc...) is een pluspunt
  • zelfstandig kunnen werken
  • teamplayer met eigen inbreng en mening
  • passen binnen agile development principle
  • ervaring met high-volumes websites

Mochten je zinnen hierdoor geprikkeld zijn, just let me know.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development, Varia

Linq to SQL out - .NetTiers in

22. May 2008

Ok, after some months of development I took a major decision and dropped the Linq data layer from our project. I had to spend to much time creating exotic lambda expressions to do the simplest things. And since I am working in a disconnected environment it all adds an extra hassle connecting the disconnected items back to the datacontext. Deletes weren't working, updates needed a detach every time we wanted to persist entities to the data store, ...

So I went on a crusade for a high performance and easy to extend data access layer. I came out to .NetTiers together with CodeSmith to generate the data layer. It also gives us the possibility to extend it all to our needs.

Goodbye Linq to SQL ... we had a good time together and I learned a lot about lambda expressions but the rest is just a hole in my memory.

BTW, I'm really into Linq to XML since that is a real progress to traversing XML docs.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,

301 Redirect in ASP.NET - The SEO friendly redirect

22. April 2008

301 redirects in ASP.NETWhen you run a website, there are times that you'll need to redirect webpages to other webpages. There are two key ways to accomplish this task, issuing a 301 redirect or a 302 redirect. What you might not know is that a 301 redirect is search engine friendly and a 302 redirect is not. 301’s will safely tell the search engines that one page has been permanently moved to a new location, while 302’s tell the search engines that it’s a temporary redirect (which can cause problems down the line.) This shouldn’t be news for anyone working in the search industry, but might be news for website owners outside of the industry.

Since most redirect in ASP.NET are 302's, we need a way to get a 301 redirect in code that is much more SEO  friendly. I included a piece of code to show you how this is done.

Add these lines of code to your Page_Load event to get a 301 redirect:

Response.Status = "301 Moved Permanently";
Response.AddHeader("Location",
http://www.your-new-location.com);
Response.End();

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,

My first Silverlight 2 app

16. April 2008

Microsoft Silverlight 2.0 BetaFinally I found some time to do some research about Silverlight 2.0. I've been working with MCML (Media Center Markup Language) for some time now, so it wasn't that big of a step.

You can find my first app in the middle column on this page where it says Pixagogo Album Viewer, just enter an album pin (you can find one one the http://www.pixagogo.com/ website by clicking an album and copying the last 10 numbers of the web address) and hit View album.

If you just want to try it, you can use this album PIN: 9126395246

I know, there is still some work to be done, but hey, I was so happy that I managed to do this that I couldn't wait to get it online.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,

Portfolio online

7. April 2008

I just put part of my portfolio online to give everybody an insight of how I spend my time. So feel free to take a look.

Also, keep an eye on the portfolio as I might post some goodies for the Pixagogo website in the near future.

Use the menu on top of this page to navigate to my portfolio or click here.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Downcast a generic List&lt;T&gt; to a base type

27. February 2008

Sometimes it is usefull to downcast a specific List<T> to a List of object that T inherited from, like an interface or so. This is the way to get the job done. 

We start with the following piece of code:

   1: // your class definition
   2: public class AlfaRomeoCar : ICar
   3: {  
   4:   ...
   5: }
   6:  
   7: // A method that only accepts List<ICar>
   8: public static List<Part> LoadAllCarPartsWithSerial(List<ICar> cars)
   9: {
  10:   ...
  11:   return resultList
  12: } 
  13:  

 

But what do we do if we need to supply a List<AlfaRomeoCar> to the LoadAllCarPartsWithSerial(List<ICar>) method? Well we can downcast the objects in the list to the base interface to use it as a parameter.

   1: List<ICar> cars = alfaCars.ConvertAll(List<AlfaRomeoCar>.DownCast<AlfaRomeoCar, ICar>());

 

And here is the DownCast converter method:

   1: public static Converter<T, U> DownCast<T, U>() where T : U
   2: { 
   3:   return delegate(T item) { return (U)item; };
   4: }

 

Et voila,I hope this helps.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,