Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating a Generic function to clone an object using extension method

    • 0
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 168
    Comment on it

    This is an extension method that operates on different types of objects and creates its own copy. This method is generic so it can clone different types of objects

    public static T Clone<T>(this T source)
        {
            var dcs = new DataContractSerializer(typeof(T));
            using (var ms = new System.IO.MemoryStream())
            {
                dcs.WriteObject(ms, source);
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                return (T)dcs.ReadObject(ms);
            }
        }
    

    If you are not familiar with extension method then please refer to my previous blog

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: