Click here to Skip to main content
13,828,588 members
Rate this:
 
Please Sign up or sign in to vote.
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 3 days ago
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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";
   
Comments
Priya-KIKO 3 days ago
   
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 3 days ago
   
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 3 days ago
   
Ok. Thank you so much.
OriginalGriff 3 days ago
   
You're welcome!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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);
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190114.1 | Last Updated 12 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100