Extensions

Web Search Extension for Visual Studio

http://visualstudiogallery.msdn.microsoft.com/a0c66cb5-5d6d-46ab-b169-273dec17eb24

I can strongly recommend this extension!

Annonser

File path to my self

Sometimes when you are working and backend with code that could be in diffrent types of context, called from a ASP.NET client or winform, unit test or something else. Using relative path to files could be a problem. This code could then be usefull.

static string AssemblyDirectory
{
get
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = newUriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
returnPath.GetDirectoryName(path);
}
}

ToDataTable extension for IEnumerable collections

The prolem I wanna solve is to in a very nice and shorhanded way create a System.Data.DataTable of any generic collection that inherits IEnumerable (every .NET Framework generic collection does this) building a table based on every read/write property(simple built in datatypes) that the items in the collection has. I thougt that Microsoft had build this extension already and included it in C# 3.0 but I thought wrong or maybe they included it in an early Beta version but removed it in the final release. Anyway it´s not so hard to write this extension using reflection. Here is my version of ToTableExtension.

public static class ToDataTableExtension
    {
        public static DataTable ToDataTable<T>(this IEnumerable<T> items) where T : new()
        {
            DataTable retVal = new DataTable();           
            T item = new T();
            Type t = item.GetType();
            retVal.TableName = "tbl" +t.Name;
            int propertyCount = t.GetProperties().Length;           
            foreach (PropertyInfo propInfo in t.GetProperties())
            {
                if (propInfo.CanRead || propInfo.CanWrite)
                    retVal.Columns.Add(new DataColumn(propInfo.Name, propInfo.PropertyType));
            }
            DataRow row = null;
            PropertyInfo p = null;
            foreach (T listItem in items)
            {
                row = retVal.NewRow();
                foreach (DataColumn col in retVal.Columns)
                {                   
                    p = t.GetProperty(col.ColumnName);
                    row[col.ColumnName] = p.GetValue(listItem, null);                   
                }
                retVal.Rows.Add(row);
            }
            return retVal;   
        }
    }

 

Sample code that uses this Extension

List<Car> cars = new List<Car>(); cars.Add(new Car() { Weight = 1199, Speed = 0 });
DataTable table = cars.ToDataTable();
table.WriteXml(Application.StartupPath + \\cars.xml);

XML result (have nothing to do with my extension but anyway)

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <tblCar>
    <Weight>1199</Weight>
    <Speed>0</Speed>
  </tblCar>
</DocumentElement>

If you realy want to test this you also need this class

public class Car { public int Weight { get; set; } public int Speed { get; set; } }

Or download sample project here