views:

48

answers:

1

How would I create a type while knowing just the name of the type in string form example...

My aspx contains this and some bindable control

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=MyEntities" DefaultContainerName="MyEntities" 
        EntitySetName="MyData">
    </asp:EntityDataSource>

Now from code I need to be able to create an instance of EntitySetName="MyData". Keep in mind I will not know these names until runtime tho. I can do EntityDataSource1.EntitySetName and this gives me the name in string form. But now I need to create and instance of it and get access to the members so I can manipulate the data. I have been trying Activator.CreateInstance and used various overloaded versions of it. Bottom line tho is I am not seeing how to do this. I keep running into trying to cast something into a type when all I have is the name of the type. Any pointers to any info to help me understand this would be much appreciated.

A: 

How about:

var entity = Activator.CreateInstance(Type.GetType("namespaceofyourtype.MyData, assemblynameofyourtype"));
Luhmann
Hmmm how to format this
Randy
yeah I tried that and sure it creates and instance of it but still leaves me with not understanding how to acess the members and change the data.
Randy
var inst = System.Activator.CreateInstance(Type.GetType("MyData"));inst.GetType() oMyData = (inst.GetType())inst;oMyData.FirstName = "Bimbo";how to do this?
Randy
var entity= Activator.CreateInstance(Type.GetType("Lottery", true, true));PropertyInfo property = entity.GetType().GetProperty("FirstName");property.SetValue(entity, "Bimbo", null);
Luhmann
Luhmann omgawd, so easy yet so hard. ty ty ty ty :)
Randy