Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to instantiate an object of a class whose name is arrived at the runtime.

var restlist = oContext.rests.ToList();
foreach(rest r in restlist)
{
     if (TableExists("bil" + r.code))
     {
           // want to instantiate an object of "bil"+r.code entity and access the 
           // properties of that class
     }
}


Is this possible this way in Entity Frawework ? Please suggest right way to do this. Thanks in advance.

What I have tried:

Tried use Activator.CreateInstance
String mfilename = "Test.bil" + r.code;
Object obj = Activator.CreateInstance(Type.GetType(mfilename));
// then here how to use this class and declare an object and get the properties
Posted
Updated 11-Jan-19 23:12pm
v4

Try this:
public class MyBaseClass
    {
    public int I;
    public string S;
    }
public class MyClass : MyBaseClass
    {
    ...
    }
private void MyButton_Click(object sender, EventArgs ew)
    {
    string myClassName = "GeneralTesting.frmMain+MyClass";
    Type t = Type.GetType(myClassName);
    MyBaseClass mbc = (MyBaseClass)Activator.CreateInstance(t);
    mbc.S = "hello";
 
Share this answer
 
Comments
Priya-Kiko 12-Jan-19 5:20am    
Thank you for the response.

The class which im trying to instantiate is a table class (or entity class) created by Entity Framework. Which class am i supposed to use as BaseClass here ??
OriginalGriff 12-Jan-19 5:39am    
That's the problem with dynamically creating types and trying to to use them from static code: unless you can provide a class to cast them to, you can't access properties, methods, fields, events, ... directly in code because you can't compile it, you need to use reflection to access them (which is slow, messy, and hard to read).
Remember, to access a class property directly in code, you need an instance of that class in an appropriately typed variable. In my example, if MyClass had a "Foo" property, you still couldn't access it via the mbc variable because not all types that can be in the variable will have a "Foo" property, they may have "Bar" instead.

It doesn't matter if this is EF or vanilla C#, unless you know what type it is at compile time (or use dynamic variables) you can't access the properties or methods except by reflection. (And dynamic only bypasses compile time checking, it will still fail if the instance doesn't have the property at run time.)
Priya-Kiko 12-Jan-19 6:50am    
Ok. Thank you so much.
OriginalGriff 12-Jan-19 7:05am    
You're welcome!
Perhaps you are not specifying fullyqualified namespace of the class to be instantiated.

string formTypeFullName = string.Format("{0}.{1}", this.GetType().Namespace, "<class_name>"); 
		Type type = Type.GetType(formTypeFullName, true);
		object obj = Activator.CreateInstance(type);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900