Sunday, October 25, 2009

SQL to LINQ Cheat Sheet

Excellent explanation how to covert SQL query to LINQ. Click HERE to article.

LINQ Syntax

Sunday, July 12, 2009

C# And Accepting Parameters

Have you ever written a function that looked similar to the following – Passing in an array of a value? To interesting solution click here.

 

via DotNetKicks.com

Wednesday, June 17, 2009

Debug windows service

In the time, when you work on development a windows service not possible to debug it. A solution is a converting service to console application or creating additional service runner or anymore. I want to present very simple, effective and easy way how to resolve this. In debug mode will run application and in not debug mode will run service.

[RunInstaller(true)]
public partial class ServiceRunner : ServiceBase
{
[STAThread]
static void Main()
{
#if !DEBUG
//Run Windows Service
Run(new ServiceRunner());
#else
//Run Application
new YourServiceBody();
#endif
}
public ServiceRunner()
{
InitializeComponent();

}
protected override void OnStart(string[] args)
{
new YourServiceBody();
}

protected override void OnStop()
{}
}

Good luck!
kick it on DotNetKicks.com

Sunday, April 05, 2009

Clone an object in C# using reflection for system and generic types

public object Clone()
{
object newObject = Activator.CreateInstance(GetType());
PropertyInfo[] propertyInfos = GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
if (propertyInfo.PropertyType.IsGenericType)
{
if (propertyInfo.PropertyType.GetInterface("IList", true) != null)
{
IList oldList = propertyInfo.GetValue(this, null) as IList;
if (oldList != null && oldList.Count > 0 &&
oldList[0].GetType().GetInterface("ICloneable", true) != null)
{
IList newList = (IList)propertyInfo.GetValue(newObject, null);
foreach (object obj in oldList)
{
ICloneable clone = (ICloneable)obj;
newList.Add(clone.Clone());
}
}
else
{
propertyInfo.SetValue(newObject, oldList, null);
}
}
if (propertyInfo.PropertyType.GetInterface("IDictionary", true) != null)
{
IDictionary oldDic =
propertyInfo.GetValue(this, null) as IDictionary;
if (oldDic != null && oldDic.Count > 0 &&
oldDic[0].GetType().GetInterface("ICloneable", true) != null)
{
IDictionary newDic =
(IDictionary)propertyInfo.GetValue(newObject, null);
foreach (DictionaryEntry entry in oldDic)
{
ICloneable clone = (ICloneable)entry.Value;
newDic[entry.Key] = clone.Clone();
}
}
else
{
propertyInfo.SetValue(newObject, oldDic, null);
}
}
}
else
{
//Clone IClonable object
if (propertyInfo.GetType().GetInterface("ICloneable", true) != null)
{
ICloneable clone = (ICloneable)propertyInfo.GetValue(this, null);
propertyInfo.SetValue(newObject, clone.Clone(), null);
}
else
{
propertyInfo.SetValue(
newObject, propertyInfo.GetValue(this, null), null);
}
}
}
return newObject;
}