locked
Creating object from string variable. RRS feed

  • Question

  • I had a string variable which contains class Name.

    ex:- string strSelection = "DNS";

    We have a class library where DNS is a public class and has two fields.

    Now, I want to create a object of above string variable and should be able to access fields of that class.  Like DNS we can get any class name at runtime depending on drop-down-list selection, basing on selection object of that type has to be created.  

                var assembly = Assembly.LoadFrom(@"C:\Users\Administrator\Documents\visual studio 2010\Projects\ReflectionTest\ReflectionTest\bin\BL.dll");
                string strSelection = "DNS";
                Type t = assembly.GetType("BL." + strSelection);

                var instance = Activator.CreateInstance(t);
                //string so = instance.GetType().Name;

                //Object o = instance.GetType().Name;

                IList<FieldInfo> props = new List<FieldInfo>(t.GetFields());

                foreach (FieldInfo prop in props)
                {

                }

    My question is how can I create object from a string variable and should be able to access public fields in that class.

    Thanks In Advance,

    Narayana.

    Monday, October 21, 2013 3:11 PM

Answers

  • Hi,

    again, you can do things either through reflection or using the dynamic type. Let's say we have to set a property named "AccountNumber":

    var instance = Activator.CreateInstance(t);
    
    var property = t.GetProperty("AccountNumber");
    property.SetValue(instance, "123");

    In this case, it makes even more sense to use dynamic:

    dynamic instance = Activator.CreateInstance(t);
    
    instance.AccountNumber = "123";

    Please notice that you have to use dynamic, not var (or object). What this does is to tell the compiler that the presence of the property cannot be verified at compile time and that the check will have to be performed at run time.

    If you are still getting compile errors, please post the code that is failing.

    HTH
    --mc

    • Marked as answer by Caillen Wednesday, October 23, 2013 6:04 AM
    Monday, October 21, 2013 10:52 PM
  • Hi Cossi, Thanks for your inputs, I implemented the code as per your inputs and it's working fine.  Thanks for your inputs/direction.

    SivaNarayana

    • Marked as answer by SivaNarayana9 Tuesday, October 22, 2013 1:03 AM
    Tuesday, October 22, 2013 1:03 AM

All replies

  • There are several ways to obtain that, depending on what you are after.

    If you just want to obtain the values, all you have to do is this:

    ...
    var instance = Activator.CreateInstance(t);
    var fields = t.GetFields();
    
    foreach (var field in fields) {
      Console.WriteLine ("{0} = {1}", field.Name, field.GetValue(instance));
    }

    A similar approach would work for properties; just use t.GetProperties() instead.

    If you know in advance that all the instances you create contain a specific field, say "foo", and that's the field you are interested in reading, you can also use a different approach:

    dynamic instance = Activator.CreateInstance(t);
    
    Console.WriteLine(instance.foo);
    HTH
    --mc

    • Proposed as answer by Wizend Monday, October 21, 2013 5:10 PM
    Monday, October 21, 2013 4:21 PM
  • Hi cossi, Thanks for the inputs, but after creating instance, say object is instance, how can I assign values to properties if I say instance.AccountNumber="123" then getting compile error.  I should be able to assign field values and read field values, reading the values I can do, but assign values getting error.

    Thanks,

    Narayana


    SivaNarayana

    Monday, October 21, 2013 9:32 PM
  • Hi,

    again, you can do things either through reflection or using the dynamic type. Let's say we have to set a property named "AccountNumber":

    var instance = Activator.CreateInstance(t);
    
    var property = t.GetProperty("AccountNumber");
    property.SetValue(instance, "123");

    In this case, it makes even more sense to use dynamic:

    dynamic instance = Activator.CreateInstance(t);
    
    instance.AccountNumber = "123";

    Please notice that you have to use dynamic, not var (or object). What this does is to tell the compiler that the presence of the property cannot be verified at compile time and that the check will have to be performed at run time.

    If you are still getting compile errors, please post the code that is failing.

    HTH
    --mc

    • Marked as answer by Caillen Wednesday, October 23, 2013 6:04 AM
    Monday, October 21, 2013 10:52 PM
  • Hi Cossi, Thanks for your inputs, I implemented the code as per your inputs and it's working fine.  Thanks for your inputs/direction.

    SivaNarayana

    • Marked as answer by SivaNarayana9 Tuesday, October 22, 2013 1:03 AM
    Tuesday, October 22, 2013 1:03 AM