1

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)
    {
    ...
sǝɯɐſ
  • 2,470
  • 4
  • 33
  • 47
  • Can you shed more light on what this "so that I can explicitly name my variable (and be able to use FmlaModel.State in other contexts)" means and in general what you are up to? – Pawel Dec 06 '12 at 00:07
  • @Pawel My apologies, please see my updated question... hopefully that will clear it up a little... – sǝɯɐſ Dec 06 '12 at 16:37
  • This seems like an XY problem to me. You want to do X, you think Y is the way to do it so you ask about Y. Why would you want to do this? What is the MAIN problem that needs to be solved? – Ryan Bennett Dec 06 '12 at 19:59
  • @RyanBennett I appreciate your comment, but I tried to clear that up with my edit... I'm not sure what else to tell you... =P Basically, I'm trying to create a generic "Table Editor" class that I can pass any of the Entities in my EF model to, so that a user can edit the table. But I can't figure out how to grab an EntityObject with a string using reflection (or any other method). – sǝɯɐſ Dec 07 '12 at 15:24
  • You could try something along these lines http://stackoverflow.com/questions/12455389/entity-framework-get-entity-by-name – Code Uniquely Jul 22 '13 at 03:51

0 Answers0