从以前thread我问怎么从一个通用的方法的ID获得注册,我得到的答案是这样的:c# - 我怎样才能从一个实例泛型类的属性?
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();
}
}
其中一期工程不错,但现在我卡住了,不知道怎么弄任何它的属性,给出一个具体的问题,我怎么能让这个函数在下面的工作?
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
}
更新解决方案
由于下面我的链接设法解决这个
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);
}
的感谢!这引导我回答 – Mol 2012-08-15 03:41:08