Create instance of the run-time type of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayane
    New Member
    • Sep 2008
    • 2

    #1

    Create instance of the run-time type of an object

    Hi everone

    I have myobject of type object and i want to create instance of its real type. All types are standart types. From myobject.getTyp e() and can get its type. But how to create instance ? If anyone know, please write an answer.

    Thanks
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    Per the example at: MSDN - Activator.Creat eInstance(Type)

    Code:
          object createdObject = Activator.CreateInstance(typeof(StringBuilder));
          StringBuilder sb = createdObject as StringBuilder;
          if (sb != null)
            sb.AppendLine("I was created dynamically.");
          Console.WriteLine(sb.ToString());
        }

    Comment

    Working...