views:

100

answers:

1

I have a linq Context that I am looking at all the data Tables, I am trying to get the list of fields in all tables

        foreach (var code in ctx.GetType().GetProperties())
        {
            Console.WriteLine(code.PropertyType + " - " + code.Name + " ");
            if (code.PropertyType.ToString().Contains("System.Data.Linq.Table"))
            {
                  //this does not give me what i want here
                  foreach (var pi in code.PropertyType.GetType().GetProperties())
                  {
                   Console.WriteLine(pi.Name);
                  }
            }
        }

That does not deliver me the columns in each table.

Any thoughts?

simplistically I am trying to get all the properties when all i have is a propertyInfo of the object i am trying to get properties for.

-Hurricane

+1  A: 
foreach (var code in ctx.GetType().GetProperties())
        {
            Console.WriteLine(code.PropertyType + " - " + code.Name + " ");
            if (code.PropertyType.ToString().Contains("System.Data.Linq.Table"))
            {
                  //this does not give me what i want here
                  foreach (var pi in code.PropertyType.GetGenericArguments()[0].GetProperties())
                  {
                   Console.WriteLine(pi.Name);
                  }
            }
        }

Change it to that. You were reflecting over System.Data.Linq.Table, but remember, the property on the DataContext is Table<T> where T is the class that represents the actual table in the database. Therefore, you need to reflect over the T instead.

BFree