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

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;
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.