0

From a previous thread I asked how to get a registry from an id in a generic way, the answer I got was this:

    public class DBAccess
{
    public virtual DataBaseTable GetById<DataBaseTable>(int id, Table<DataBaseTable> table) where DataBaseTable : class
    {
        var itemParameter = Expression.Parameter(typeof(DataBaseTable), "item");
        var whereExpression = Expression.Lambda<Func<DataBaseTable, bool>>
            (
            Expression.Equal(
                Expression.Property(
                    itemParameter,
                    "Id"
                    ),
                Expression.Constant(id)
                ),
            new[] { itemParameter }
            );
        return table.Where(whereExpression).Single();
    }
}

Which works nice, but now I'm stuck not knowing how to get any of its attributes, to give a concrete question, how could I make this below function work?

    public static int GetRelatedTableId<DataBaseTable>
    (Table<DataBaseTable> table,String RelatedTableId) where DataBaseTable : class
    {
        DBAccess RegistryGet = new DBAccess();    
        DataBaseTable tab = RegistryGet.GetById<DataBaseTable>(id, table);
        return tab.getAttributeByString(RelatedTableId);//i just made this up
    }

Update Solution

Thanks to the link below i managed to solve this

    public static int GetRelatedTableId<DataBaseTable>
    (Table<DataBaseTable> table,String RelatedTableId) where DataBaseTable : class
    {
        DBAccess RegistryGet = new DBAccess();    
        DataBaseTable tab = RegistryGet.GetById<DataBaseTable>(id, table);
        return (int)(tab.GetType().GetProperty(RelatedTableId)).GetValue(tab, null);
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mol
  • 17
  • 4

2 Answers2

0

If your property name is only known at runtime, then you can use reflection to inspect the type DataBaseTable, find the property of interest by name, and then retrieve its value from your instance tab.

See the answer to this question for an example: How can I get the value of a string property via Reflection?

Clarification: Yes, your type is a generic argument, but typeof(DataBaseTable) or tab.GetType() will still allow you to inspect the particular type being used.

Community
  • 1
  • 1
Michael Petito
  • 12,891
  • 4
  • 40
  • 54
0

If you only know the property name at run time use reflection... though this is a code smell more often than not.

If you know the property name but not the concrete type at compile time (and are using .net 4) cast the return value to a dynamic and access the property normally.

If you know the concrete type and property at compile time cast the return value to the returned type and access the property normally.

also based on the supplied fragment your code should probably either be

public class DBAccess<T> where T : class
{
    public virtual T GetById(int id, Table<T> table)
    {

or

public static class DBAccess 
{
    public static T GetById<T>(int id, Table<T> table) where T : class
    {
Yaur
  • 7,333
  • 1
  • 25
  • 36
  • I disagree that using reflection is a code smell more often than not. You can find reflection used in many widely used and well thought out libraries. It's a tool like any other. – Michael Petito Aug 15 '12 at 15:25