I am trying to figure out how to select values from a lookup table using generics and at run time pass the entity name as a string. Here is what I have so far. The problem I'm having is in the "var codes = Select();" line. As you can guess, the compiler is not happy and is giving me an error of: classType is a variable but used as a type. I'm not sure what to do or how to fix it. I can't hardcode an interface or class name as I don't know the signature of the code table until the string is resolved. Any guidance would be greatly appreciated!
public void GetTableNameObject(string TableName)
{
dynamic classType = Activator.CreateInstance(Type.GetType(TypeName));
var codes = Select<classType>();
}
public IQueryable<TItem> Select<TItem>() where TItem : class, new()
{
PropertyInfo property = GetDbSet(typeof(TItem));
DbSet<TItem> set = property.GetValue(_context, null) as DbSet<TItem>;
return set;
}
private PropertyInfo GetDbSet(Type itemType)
{
var properties = typeof(CodeTableContext).GetProperties().Where(item => item.PropertyType.Equals(typeof(DbSet<>).MakeGenericType(itemType)));
return properties.First();
}