Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have several classes in a asp.net project.

I also have a variable which receives a class name as string.

I need to actually match or convert the string value of the variable with the corresponding class name.

What I have tried:

For example, I have classes as flollows:
C#
class Customer
class Vendor
class Importer

and a variable:
C#
var className;

If className = "Vendor", I want to actually make the string "Vendor" as Vendor class.

what is the simplest way to do it?
Posted
Updated 26-May-20 1:15am

 
Share this answer
 
Comments
TempoClick 26-May-20 9:41am    
Thanks for the reply. How can I use the class methods afterwards?
F-ES Sitecore 26-May-20 11:38am    
Use reflection to query methods\properties to see if they exist, and you can call them via reflection too.

If there is any commonality between your classes then have them implement a common interface and call the methods through the interface.
With Reflection like this:

class Program
    {
        class Customer { }
        class Vendor { }
        class Importer { }
        static void Main(string[] args)
        {
            string strClassName = nameof(Importer); // Use Full typename
            if (strClassName == nameof(Importer))
            {
                Importer importerInstance = Activator.CreateInstance(typeof(Importer)) as Importer;
            }
            else if (strClassName == nameof(Vendor))
            {
                Vendor importerInstance = Activator.CreateInstance(typeof(Vendor)) as Vendor;
            }
            // ...
        }
    }
 
Share this answer
 
Comments
TempoClick 26-May-20 9:43am    
Thanks for the reply. How can I avoid the if/else statements? I have too many classes!
johannesnestler 26-May-20 10:50am    
This was just an example how to use Activator. You could do it like this:

class Program
{
class Customer { }
class Vendor { }
class Importer { }
static void Main(string[] args)
{
string strClassName = "ConsoleApp10.Program+Customer"; // note the + in the full type name (it's because I embeded thy type inside the "Program"-class in this example)

Assembly assembly = Assembly.GetAssembly(typeof(Program)); // Get your assembly
Type type = assembly.GetType(strClassName); // search type inside assembly
object objInstance = Activator.CreateInstance(type);

Console.ReadKey();
// ...
}
}
TempoClick 26-May-20 11:31am    
In your example I will receive the class Customer via objInstance as object. What if I want then to use a method GetNames() in the class Customer? How should I call it having the object objInstance?
johannesnestler 28-May-20 3:04am    
using reflection again or "dynamic" or cast to real type (check with "is" or "as" Operator). May I ask what is your "real" Problem (so why you have to do this), maybe there are other solutions - because solutions that are based on strings and heavy reflection are error prone and performance heavy...
johannesnestler 28-May-20 3:14am    
new example:

class Program
{
class Customer { public void DoTheCustomerThing() { Console.WriteLine("Customer"); } }
class Vendor { public void DoTheVendorThing() { } }
class Importer { public void DoTheImporterThing() { } }
static void Main(string[] args)
{
string strClassName = "ConsoleApp10.Program+Customer"; // note the + in the full type name (it's because I embeded thy type inside the "Program"-class in this example)

Assembly assembly = Assembly.GetAssembly(typeof(Program)); // Get your assembly
Type type = assembly.GetType(strClassName); // search type inside assembly
object objInstance = Activator.CreateInstance(type);

//use instance
// 1. Option cast
// ... (with "is" operator)
if(objInstance is Customer customer)
{
customer.DoTheCustomerThing();
}
// 2. dynamic (check for runtime errors! no compiler check!) somehow you have to know what type the instance has, and what methods are available
try
{
dynamic dynInstance = objInstance;
dynInstance.DoTheCustomerThing();
//dynInstance.CallAMethodThatIsNotAvailableLeadsToRuntimeBinderException(); // this gives you an exception
}
catch(RuntimeBinderException)
{
// Method doesn't exist on type
}
// 3. using reflection again
MethodInfo mi = type.GetMethod("DoTheCustomerThing");
mi.Invoke(objInstance, null);

Console.ReadKey();
// ...
}

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