1

I have a string and need to convert it into a class Name.

For example, I have the class :

public class Account 
{
    public double Quantity { get; set; }    
    public double Amount{ get; set; }
}

And I need to convert:

String x = "Account"

to the name of that class

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Have a look at `Type.GetType` – Dmitry Bychenko Oct 10 '16 at 15:16
  • 4
    *Why* do you want that? What is the *actual* problem you are trying to solve? You can use reflection to create types, invoke methods by name but it's rather cumbersome. Usually, there are far easier ways to do what you want, eg use an IoC container, or a Factory method to generate instances based on some parameter – Panagiotis Kanavos Oct 10 '16 at 15:16
  • this is an example : I have this method : IList List() where T : class; String X = "account" ; var Y = convert the string to an instance of the class. I want to have this : var accounts = DataAccessServicesProvider.List(); – aous elkadhi Oct 10 '16 at 15:42
  • In c# 6.0 you can use new nameof operator : `var s = nameof(MyClass);` – Fabjan Oct 10 '16 at 15:43
  • But i work with c# 4.0.Thank you guys – aous elkadhi Oct 11 '16 at 07:00

0 Answers0