以前のスレッドから、一般的な方法で ID からレジストリを取得する方法を尋ねたところ、得られた答えは次のとおりです。
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);
}