Dynamically creating an object (Reflection?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MarkusJNZ@gmail.com

    Dynamically creating an object (Reflection?)

    Hi, given a name of an class e.g. "MyClass" which is stored as a string
    is it possible to create a new instance of this type dynamically and
    then reference the attributes by name in code??

    e.g.

    If my class is called Person and it has attributes FirstName and
    LastName is it possible to do something like this;

    public void GeneratePerson( )
    {

    string className = "Person";
    (CreateInstance OfPerson) _person = new (CreateInstance OfPerson);
    _person.FirstNa me = "fred";
    _person.LastNam e = "smith";
    _person.AddName ();

    }

    I did look at using reflection but can't seem to create an instance of
    an object just given a string and then populate the object with
    attrbutes.

    Any help appreciated
    Thanks
    Markus
    =============== ========
    googlenews2006m arkusj

  • Jon Skeet [C# MVP]

    #2
    Re: Dynamically creating an object (Reflection?)

    <MarkusJNZ@gmai l.comwrote:
    Hi, given a name of an class e.g. "MyClass" which is stored as a string
    is it possible to create a new instance of this type dynamically and
    then reference the attributes by name in code??
    >
    e.g.
    >
    If my class is called Person and it has attributes FirstName and
    LastName is it possible to do something like this;
    >
    public void GeneratePerson( )
    {
    >
    string className = "Person";
    (CreateInstance OfPerson) _person = new (CreateInstance OfPerson);
    _person.FirstNa me = "fred";
    _person.LastNam e = "smith";
    _person.AddName ();
    >
    }
    >
    I did look at using reflection but can't seem to create an instance of
    an object just given a string and then populate the object with
    attrbutes.
    You want Activator.Creat eInstance to create the instance - or use
    Type.GetType to get the type, and then fetch and invoke a constructor.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • MarkusJNZ@gmail.com

      #3
      Re: Dynamically creating an object (Reflection?)

      Thanks Jon
      Regards
      Markus
      Jon wrote:
      <MarkusJNZ@gmai l.comwrote:
      Hi, given a name of an class e.g. "MyClass" which is stored as a string
      is it possible to create a new instance of this type dynamically and
      then reference the attributes by name in code??

      e.g.

      If my class is called Person and it has attributes FirstName and
      LastName is it possible to do something like this;

      public void GeneratePerson( )
      {

      string className = "Person";
      (CreateInstance OfPerson) _person = new (CreateInstance OfPerson);
      _person.FirstNa me = "fred";
      _person.LastNam e = "smith";
      _person.AddName ();

      }

      I did look at using reflection but can't seem to create an instance of
      an object just given a string and then populate the object with
      attrbutes.
      >
      You want Activator.Creat eInstance to create the instance - or use
      Type.GetType to get the type, and then fetch and invoke a constructor.
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Steven Nagy

        #4
        Re: Dynamically creating an object (Reflection?)

        You also won't be able to get this kind of strong typing in your code:

        _person.FirstNa me = "fred";

        Objects created using reflection can have their properties and methods
        referenced through a collection however. You can once again use the
        string name of the field in order to find it. This code should be
        pretty well documented in MSDN.

        However, if at design time you know for sure the names of the
        properties and methods (but not the class name), you might be able to
        extract your common class aspects into an interface, then cast the
        dynamically created object as an interface instance, and then use the
        strongly typed names.

        Hope this makes sense.

        Steven

        Comment

        Working...