Source

main / Source / Excel / Excel.Common / Extensions / ObjectExtensions.cs

Full commit
Naethra D ba1cb57 






































































using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace DataUp.Excel.Common
{
    /// <summary>
    /// Class having the extension methods needed for all Objects.
    /// </summary>
    public static class ObjectExtensions
    {
        /// <summary>
        /// Checks whether the given parameters are null or not. In case of null, throws Argument null exception.
        /// </summary>
        /// <typeparam name="T">Type of object on which check method is called</typeparam>
        /// <param name="o">Object on which check method is called</param>
        /// <param name="parameters">List of parameters (generally objects) to be checked for null</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "We are validating this parameter using reflection."),
        System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "o", Justification = "Need to have check null for all objects.")]
        public static void CheckNotNull<T>(this T o, Func<object> parameters)
        {
            var objects = parameters();
            var properties = objects.GetType().GetProperties();

            // Find the parameters/objects which are null and get their name.
            var emptyProperties = properties.Where(p => p.GetValue(objects, null) == null).Select(p => p.Name);

            if (emptyProperties.Count() > 0)
            {
                throw new Exception("Item Not Exists");
            }
        }

        /// <summary>
        /// This method can be used to serialize any object using DataContractSerializer.
        /// </summary>
        /// <typeparam name="T">Type of the parameter.</typeparam>
        /// <param name="obj">Object on which check method is called</param>
        /// <param name="rootName">Name of the root in the serialized XML.</param>
        /// <returns>Serialized string representation of the object.</returns>
        public static string SerializeObject<T>(this T obj, string rootName)
        {
            if (obj != null)
            {
                DataContractSerializer se;
                if (string.IsNullOrWhiteSpace(rootName))
                {
                    se = new DataContractSerializer(typeof(T));
                }
                else
                {
                    se = new DataContractSerializer(typeof(T), rootName, string.Empty);
                }

                byte[] bArr;
                using (MemoryStream ms = new MemoryStream())
                {
                    se.WriteObject(ms, obj);
                    ms.Position = 0;
                    bArr = new byte[ms.Length];
                    ms.Read(bArr, 0, Convert.ToInt32(ms.Length));
                }

                return new UTF8Encoding().GetString(bArr);
            }

            return null;
        }
    }
}