Create an object on the fly knowing just an object type

Discussion in 'ASP .Net' started by Just D., May 7, 2005.

  1. Just D.

    Just D. Guest

    All,

    Do we have a simple way to Create an object on the fly knowing just an
    object type? The usual design-time way is to write a code something like
    this:

    CObjectType obj = new CObjectType();

    That's simple. But to create any object knowing its object type on the fly
    is looking like a problem. I'll try to explain the idea.

    We have some base class with the method:

    Deserialize(string sXML, ref obj, System.Type systemtype)
    {
    ....
    }

    The idea is to create any object of required type, the type should be
    defined through the interface, then to do some appropriate steps to get the
    data from the XML string and assign all required properties of the created
    object. The idea is simple, but realization... The worst scenario is to use
    a switch by systemtype and create all required objects inside an appropriate
    switch case. The method is terrible because it's not nice, looking terribly
    and absolutely not flexible. But to do the method universal we need to
    create a new object on the fly using something like:

    systemtype obj = new systemtype();

    where "systemtype" is the variable received as a method parameter.

    The problem that writing this we're getting a non-workable code.

    Any idea or solution? Maybe we could use a virtual method to do this thing
    or some other trick?

    Thanks,
    Just D.
     
    Just D., May 7, 2005
    #1
    1. Advertising

  2. Just D,

    I would look into System.Reflection.Assembly.CreateInstance.

    Hope this helps.


    "Just D." wrote:

    > All,
    >
    > Do we have a simple way to Create an object on the fly knowing just an
    > object type? The usual design-time way is to write a code something like
    > this:
    >
    > CObjectType obj = new CObjectType();
    >
    > That's simple. But to create any object knowing its object type on the fly
    > is looking like a problem. I'll try to explain the idea.
    >
    > We have some base class with the method:
    >
    > Deserialize(string sXML, ref obj, System.Type systemtype)
    > {
    > ....
    > }
    >
    > The idea is to create any object of required type, the type should be
    > defined through the interface, then to do some appropriate steps to get the
    > data from the XML string and assign all required properties of the created
    > object. The idea is simple, but realization... The worst scenario is to use
    > a switch by systemtype and create all required objects inside an appropriate
    > switch case. The method is terrible because it's not nice, looking terribly
    > and absolutely not flexible. But to do the method universal we need to
    > create a new object on the fly using something like:
    >
    > systemtype obj = new systemtype();
    >
    > where "systemtype" is the variable received as a method parameter.
    >
    > The problem that writing this we're getting a non-workable code.
    >
    > Any idea or solution? Maybe we could use a virtual method to do this thing
    > or some other trick?
    >
    > Thanks,
    > Just D.
    >
    >
    >
     
    =?Utf-8?B?WWU=?=, May 7, 2005
    #2
    1. Advertising

  3. System.Reflection.Assembly.CreateInstance is one way, notice that you need
    to know the assembly in which the object resides.

    If your Deserialize method allows for external objects, you will need to
    save the name of the assembly as well as the name of the type, you can then
    use Assembly.Load to load the assembly after which you can use
    Assembly.CreateInstance.

    There are other ways to create an object given its type, you can use
    Assembly.GetType() which return you a Type object, from this Type object you
    can obtain a constructor by using Type.GetConstructor, you can also use the
    Type object to call any method after the instance is created.

    Check out the System.Reflection namespace, you will find tons of stuff
    there.

    --
    Francisco Padron
    www.chartfx.com


    "Ye" <> wrote in message
    news:...
    > Just D,
    >
    > I would look into System.Reflection.Assembly.CreateInstance.
    >
    > Hope this helps.
    >
    >
    > "Just D." wrote:
    >
    >> All,
    >>
    >> Do we have a simple way to Create an object on the fly knowing just an
    >> object type? The usual design-time way is to write a code something like
    >> this:
    >>
    >> CObjectType obj = new CObjectType();
    >>
    >> That's simple. But to create any object knowing its object type on the
    >> fly
    >> is looking like a problem. I'll try to explain the idea.
    >>
    >> We have some base class with the method:
    >>
    >> Deserialize(string sXML, ref obj, System.Type systemtype)
    >> {
    >> ....
    >> }
    >>
    >> The idea is to create any object of required type, the type should be
    >> defined through the interface, then to do some appropriate steps to get
    >> the
    >> data from the XML string and assign all required properties of the
    >> created
    >> object. The idea is simple, but realization... The worst scenario is to
    >> use
    >> a switch by systemtype and create all required objects inside an
    >> appropriate
    >> switch case. The method is terrible because it's not nice, looking
    >> terribly
    >> and absolutely not flexible. But to do the method universal we need to
    >> create a new object on the fly using something like:
    >>
    >> systemtype obj = new systemtype();
    >>
    >> where "systemtype" is the variable received as a method parameter.
    >>
    >> The problem that writing this we're getting a non-workable code.
    >>
    >> Any idea or solution? Maybe we could use a virtual method to do this
    >> thing
    >> or some other trick?
    >>
    >> Thanks,
    >> Just D.
    >>
    >>
    >>
     
    news.microsoft.com, May 7, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harvey
    Replies:
    1
    Views:
    294
    Scott Allen
    Aug 19, 2004
  2. RCS
    Replies:
    2
    Views:
    419
  3. Nitin

    Knowing size of a data type

    Nitin, Sep 2, 2003, in forum: C Programming
    Replies:
    18
    Views:
    590
    Bob Jacobs
    Sep 13, 2003
  4. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    111
    Brian
    Apr 8, 2005
  5. Dan Gelder

    knowing the mime type of downloaded image

    Dan Gelder, Jan 28, 2008, in forum: Javascript
    Replies:
    2
    Views:
    94
    Dan Gelder
    Jan 29, 2008
Loading...

Share This Page