Difference between revisions of "C"

From Richard's Wiki
Jump to: navigation, search
(Get Property Name via Lambda Expression)
Line 57: Line 57:
 
             return xmlData;
 
             return xmlData;
 
         }
 
         }
 +
 +
===== TimingScope =====
 +
 +
<code>
 +
namespace IrisServices.Common
 +
{
 +
    public class TimingScope : IDisposable
 +
    {
 +
        private readonly log4net.ILog _logger;
 +
        private readonly string _name;
 +
        private readonly Stopwatch _timer;
 +
 +
        public TimingScope(string name, log4net.ILog logger)
 +
        {
 +
            _name = name;
 +
            _logger = logger;
 +
            _timer = new Stopwatch();
 +
        }
 +
 +
        public void Start()
 +
        {
 +
            _logger.DebugFormat("{0} starting", _name);
 +
            _timer.Start();
 +
        }
 +
 +
        public static TimingScope StartNew(string name, log4net.ILog logger)
 +
        {
 +
            var scope = new TimingScope(name, logger);
 +
            scope.Start();
 +
            return scope;
 +
        }
 +
 +
        public static TimeSpan Time(string name, log4net.ILog logger, Action operation)
 +
        {
 +
            using (var timer = StartNew(name, logger))
 +
            {
 +
                operation();
 +
                return timer.Elapsed;
 +
            }
 +
        }
 +
 +
        public void Stop()
 +
        {
 +
            _timer.Stop();
 +
            _logger.DebugFormat("{0} completed in {1:0.000} s", _name, _timer.ElapsedMilliseconds / 1000.0);
 +
        }
 +
 +
        public void Dispose()
 +
        {
 +
            Stop();
 +
        }
 +
 +
        public TimeSpan Elapsed
 +
        {
 +
            get { return _timer.Elapsed; }
 +
        }
 +
    }
 +
}
 +
</code>

Revision as of 18:12, 26 October 2015

Get Property Name via Lambda Expression

To use it do something like this.PropertyName(()=>this.Property);

using System;
using System.Linq.Expressions;
using System.Reflection;
private string PropertyName<T>(Expression<Func<T>> property)
{
    var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
    if (propertyInfo == null)
    {
      throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
    }
    var propertyName = propertyInfo.Name;
    return propertyName;
}

SerializeToXml
       using System.IO;
       using System.Xml;
       /// <summary>
       /// Serializes to XML.
       /// </summary>
       /// <param name="firstClassElement">The first class element to serialize.</param>
       /// <returns>The XML string that represents the passed first class element.</returns>
       public virtual string SerializeToXml(FirstClassElementType firstClassElement)
       {
           string xmlData;
           var memoryStream = new MemoryStream();
           XmlTextWriter writer = null;
           try
           {
               writer = new XmlTextWriter(memoryStream, Encoding.Unicode)
                            {
                                Formatting = Formatting.Indented,
                                Indentation = 0
                            };
               var xmlSerializer = new XmlSerializer(typeof (FirstClassElementType));
               xmlSerializer.Serialize(writer, firstClassElement);
               writer.Flush();
           }
           finally
           {
               memoryStream.Seek(0, SeekOrigin.Begin);
               var textReader = new StreamReader(memoryStream);
               xmlData = textReader.ReadToEnd();
               textReader.Close();
               if (null != writer)
                   writer.Close();
           }
           return xmlData;
       }
TimingScope

namespace IrisServices.Common {

   public class TimingScope : IDisposable
   {
       private readonly log4net.ILog _logger;
       private readonly string _name;
       private readonly Stopwatch _timer;
       public TimingScope(string name, log4net.ILog logger)
       {
           _name = name;
           _logger = logger;
           _timer = new Stopwatch();
       }
       public void Start()
       {
           _logger.DebugFormat("{0} starting", _name);
           _timer.Start();
       }
       public static TimingScope StartNew(string name, log4net.ILog logger)
       {
           var scope = new TimingScope(name, logger);
           scope.Start();
           return scope;
       }
       public static TimeSpan Time(string name, log4net.ILog logger, Action operation)
       {
           using (var timer = StartNew(name, logger))
           {
               operation();
               return timer.Elapsed;
           }
       }
       public void Stop()
       {
           _timer.Stop();
           _logger.DebugFormat("{0} completed in {1:0.000} s", _name, _timer.ElapsedMilliseconds / 1000.0);
       }
       public void Dispose()
       {
           Stop();
       }
       public TimeSpan Elapsed
       {
           get { return _timer.Elapsed; }
       }
   }

}