Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Convert dynamic string to existing Class [duplicate]

Within a windows app, using C#, I have a reporting module that will be reliant upon classes to populate the reports. However there will be many reports and I do not want to have to code for each one.

The flow will be as such: Within the report editor, the report will be assigned a class (i.e. "Applications") as a string. When the user selected the report to run, the code will acquire the data from a SQL query. The code will take the data and find out which class to place the data into. Then the report will take the class and populate the report with the data from the class.

Here is my dilemna, how do I make the code dynamic so that the code will convert the assigned class into the proper Class Object?

Example in mind:

gVar = Report;
(gVar.ReportClass)oClass = new gVar.ReportClass;
like image 826
mattgcon Avatar asked Feb 02 '11 15:02

mattgcon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

Use Type.GetType (specifically one of the overloads (e.g., Type.GetType(string)) that takes a string parameter) to load the instance of Type for the appropriate class, and then use Activator.CreateInstance or Type.GetConstructor on that instance of Type to instantiate an instance.

So, something like

Type type = Type.GetType(assemblyQualifiedName);
object instance = Activator.CreateInstance(type);

Note that you must pass the assembly qualified name unless the type is in mscorlib or the currently executing assembly.

Additionally, Activator.CreateInstance assumes the existence of a default constructor. If there is not a default constructor, or you need to pass some parameters to the constructor, you will have to use an overload of Activator.CreateInstance that lets you specify the constructor parameters, or Type.GetConstructor to load the appropriate constructor.

like image 69
jason Avatar answered Sep 29 '22 01:09

jason


You can use reflection to do it. If you give them all some similar base class or interface, you can do something like:

myBaseReport report = (myBaseReport)System.Activator.CreateInstance("MyAssemblyName", myClassStringWithFullNameSpace).Unwrap();

This will go into the assembly named and load the class directly. The class string is the full name of the type in question, so something like MyGlobalNamespace.MyCustomNameSpace.MySpecificType. This will allow you to create the specific type of report and put it into the base class type or interface type.

like image 43
Joel Etherton Avatar answered Sep 29 '22 00:09

Joel Etherton