Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get instantiated object from a string

How to get instantiated object from a string

Scheduled Pinned Locked Moved C#
helptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Al Beback
    wrote on last edited by
    #1

    I need to use Reflection to retrieve the properties of a given object. That's easy. The problem is that I don't know which object to use until run-time. The object is already available and I can't use Reflection to re-create it since its constructor is not available. Let me clarify with some code:

    object obj = GetInstantiatedObject(objectName);

    Type type = obj.GetType();
    foreach (PropertyInfo property in type.GetProperties())
    {
    ....
    }

    Does anyone know what object GetInstantiatedObject(string objectName) should contain? Thanks in advance! Al

    - Is God willing to prevent evil, but not able? Then he is impotent. - Is he able, but not willing? Then he is malevolent. - Is he both able and willing? Whence then is evil? - Is he neither able nor willing? Then why call him God? Epicurus

    S 1 Reply Last reply
    0
    • A Al Beback

      I need to use Reflection to retrieve the properties of a given object. That's easy. The problem is that I don't know which object to use until run-time. The object is already available and I can't use Reflection to re-create it since its constructor is not available. Let me clarify with some code:

      object obj = GetInstantiatedObject(objectName);

      Type type = obj.GetType();
      foreach (PropertyInfo property in type.GetProperties())
      {
      ....
      }

      Does anyone know what object GetInstantiatedObject(string objectName) should contain? Thanks in advance! Al

      - Is God willing to prevent evil, but not able? Then he is impotent. - Is he able, but not willing? Then he is malevolent. - Is he both able and willing? Whence then is evil? - Is he neither able nor willing? Then why call him God? Epicurus

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Al Beback wrote:

      The object is already available and I can't use Reflection to re-create it since its constructor is not available.

      I don't really understand that part - you already have an object, why not just use it?

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      A 1 Reply Last reply
      0
      • S S Senthil Kumar

        Al Beback wrote:

        The object is already available and I can't use Reflection to re-create it since its constructor is not available.

        I don't really understand that part - you already have an object, why not just use it?

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        A Offline
        A Offline
        Al Beback
        wrote on last edited by
        #3

        S. Senthil Kumar wrote:

        I don't really understand that part - you already have an object, why not just use it?

        Because it's not just one. Let's say that it can be up to 10 (or more), and I don't know which one I will be using until the program is run. So I can do this: object obj; if (name == "Object1") obj = Object1; else if (name == "Object2) obj = Object2; etc... Then if more objects come into the picture, I have to modify this code. I'm wondering if there's a way to use reflection to avoid doing all of that. Thanks, Al

        - Is God willing to prevent evil, but not able? Then he is impotent. - Is he able, but not willing? Then he is malevolent. - Is he both able and willing? Whence then is evil? - Is he neither able nor willing? Then why call him God? Epicurus

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups