Get Entity Object from string using reflection

2.2k Views Asked by At

I am able to obtain an object set with this code:

string tableName = "States";
var test = db.GetType().GetProperty(tableName).GetValue(db, null);

//this returns {System.Data.Objects.ObjectSet<FmlaModel.State>}

However, I would like to first find the EntityObject FmlaModel.State, so that I can explicitly name my variable type (like ObjectSet<EntityObject> instead of var), and so that I can use this to tell my form (TableEditor, see below) what type of EntityObject we are using.

Is there a way I can do this using reflection? I have searched quite a bit, but have not found anything that works yet...


EDIT: My goal is to be able to have a db table containing the names of entity objects we will be able to edit. The names will be loaded into a combobox, and when one is selected, I will create an instance of my "Table Editor" form with something like

TableEditor<myEntityObject> tableEditor 
     = new TableEditor<myEntityObject>(myEntitySet<myEntityObject>);

My "Table Editor" class looks like this:

public partial class TableEditor<TEntity> : Form
    where TEntity : EntityObject
{
    public TableEditor(ObjectSet<TEntity> something)
    {
    ...
0

There are 0 best solutions below