Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create an object of <Type>

Tags:

c#

.net

dynamic

I have a table in my database that I use to manage relationships across my application. it's pretty basic in it's nature - parentType,parentId, childType, childId... all as ints. I've done this setup before, but I did it with a switch/case setup when I had 6 different tables I was trying to link. Now I have 30 tables that I'm trying to do this with and I would like to be able to do this without having to write 30 case entries in my switch command.

Is there a way that I can make reference to a .Net class using a string? I know this isn't valid (because I've tried several variations of this):

Type t = Type.GetType("WebCore.Models.Page"); object page = new t(); 

I know how to get the Type of an object, but how do I use that on the fly to create a new object?

like image 596
thaBadDawg Avatar asked Feb 23 '09 17:02

thaBadDawg


People also ask

How do you create a dynamic object?

You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.

How do I create a dynamic object in C++?

In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically.

How do I create a dynamic object in Python?

Python Code can be dynamically imported and classes can be dynamically created at run-time. Classes can be dynamically created using the type() function in Python. The type() function is used to return the type of the object. The above syntax returns the type of object.

How do I create a dynamic object in node JS?

To create a dynamic object in a loop with JavaScript, we can use the square bracket notation to add properties to an object dynamically. We create an empty object and assign it to the objects variable. Then we add a for loop that loops through some numbers. And we use the numbers as property names with objects[x] .


2 Answers

This link should help:
https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance

Activator.CreateInstance will create an instance of the specified type.

You could wrap that in a generic method like this:

public T GetInstance<T>(string type) {     return (T)Activator.CreateInstance(Type.GetType(type)); } 
like image 71
Jason Miesionczek Avatar answered Sep 21 '22 13:09

Jason Miesionczek


If the type is known by the caller, there's a better, faster way than using Activator.CreateInstance: you can instead use a generic constraint on the method that specifies it has a default parameterless constructor.

Doing it this way is type-safe and doesn't require reflection.

T CreateType<T>() where T : new() {    return new T(); } 
like image 31
Judah Gabriel Himango Avatar answered Sep 18 '22 13:09

Judah Gabriel Himango