Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Class Name as parameter Object

Posted on 2004-04-26
3
Medium Priority
?
1,689 Views
Last Modified: 2010-05-18
This question is related to C#.
I need to create objects of the class name which i will pass as parameter. Which is very much same as RUNTIME_CLASS macro in MFC. My requirement is i want to implement the same thing in C# without using the pointers. I am including a sample code which i am using in my project

switch (classname)
{
    case "Class1"
                     Class1 obj1 = new Class1()
                     break;
    case "Class2"
                     Class2 obj2 = new Class2()
                     break;

}
                 
I want to remove the switch statement and I want to use some thing like RUNTIME_CLASS. But I want to stick with managed code.

0
Comment
Question by:sandy ware
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10916130
If you are on C# .NET, you can use this for making objects of any class:

using System.Reflection ;

..
..

Assembly a = Assembly.Load ( assemblyName ) ;
// catch FileNotFoundException - if it is thrown, then the assembly could not be loaded

// You can add: Type t = a.getType ( className, false, true ) ; if ( t == null ) -> Class cannot be instantiated

object instance = a.CreateInstance ( className ) ;

// Now you can cast it to any appropriate instance if you want.

Class1 obj = ( Class1 ) instance ;

It'd be better to check:

if ( instance is Class1 )
  Class1 obj = ( Class1 ) instance ;
0
 

Author Comment

by:sandy ware
ID: 10916760
I have tried this code and the Createinstance is always returns null.

object instance = a.CreateInstance ( className ) ; // instance is always null

 I tried with other assembly functions like, GetAssembly and GetExecutableAssembly and all those stuff. But same result.  MyApplication is a simple Exe and it not refers to any other DLL. Only thing it is having different name spaces. I tried by giving a class name which have name space which is equal to application exe name. That's also not working. The Assembly.Load function getting the assembly properly which i can view through watch window.

Please help me
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 10916976
The className should contain the complete name, something like: "namespace.sub1.sub2.ClassName". Did you give it that way?

If you have:

namespace X.Y.Z
{
  public class A
  {

The className should be: "X.Y.Z.A"
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question