how to create instance after passing class type to function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mlev

    #1

    how to create instance after passing class type to function

    I want function in one namespace to create instance of class defined in
    another namespace.
    (more detailed - I try to make independent "Seralizer" dll, to be used
    by different application, each calling it from its own class which
    defines the application configuration)
    I failed to make it work.
    Notify that in the Seralizer source I dont know the definition of the
    class I get.
    Foe example, how do I make lines like below working?
    public void Deserialize(Typ e cfgType)
    {
    myType obj = Activator.Creat eInstance(cfgTy pe);
    XmlSerializer serializer = new XmlSerializer( cfgType);
    FileStream stream = new FileStream("som e file...", FileMode.Open,
    FileAccess.Read );
    cfgType configuration = (cfgType)serial izer.Deserializ e(stream);
    }
    thanks

  • Jon Skeet [C# MVP]

    #2
    Re: how to create instance after passing class type to function

    mlev <mlev@nds.com > wrote:[color=blue]
    > I want function in one namespace to create instance of class defined in
    > another namespace.
    > (more detailed - I try to make independent "Seralizer" dll, to be used
    > by different application, each calling it from its own class which
    > defines the application configuration)
    > I failed to make it work.
    > Notify that in the Seralizer source I dont know the definition of the
    > class I get.
    > Foe example, how do I make lines like below working?
    > public void Deserialize(Typ e cfgType)
    > {
    > myType obj = Activator.Creat eInstance(cfgTy pe);
    > XmlSerializer serializer = new XmlSerializer( cfgType);
    > FileStream stream = new FileStream("som e file...", FileMode.Open,
    > FileAccess.Read );
    > cfgType configuration = (cfgType)serial izer.Deserializ e(stream);
    > }[/color]

    It's not clear what your code is meant to do - why are you bothering
    with the first line, when you're then not using "obj"? What do you
    really want the last line to do? A cast is (usually) a compile-time
    concept only - what do you want to do with the configuration variable
    at the end of the method?

    --
    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

    • Michael Lev

      #3
      Re: how to create instance after passing class type to function

      Jon,
      I want it as independent dll, which gets instance of the top class of
      some configuration (with all sub classes and values within). That
      compiled dll code knows nothing about that configuration classes
      hierarchy, yet I want it to be able to work on it, create new instance
      etc. Each of the specific code lines in my original question is only
      some example of attemts to create instances, which seem to fail.

      Specifically, I want
      1. to read the original config file, compare its values with the values
      in the parameter instance of the configuration hierarchy class and if
      modified, ask user whether to save.
      2. to merge the runtime values into the original xml config file,
      keeping its comment and blank lines.
      I want to implement these 2 tasks in an independent dll which has no
      pre-knowledge on the configuratio, so I think that one way is to pass
      the top hierarchy class type, but I dont succeed to create instances,
      neither am I sure if I can traverse the hierarchy of the input object.



      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • micha

        #4
        Re: how to create instance after passing class type to function

        Jon,
        I'll try to be much more specific:
        next 2 functions belong to class which knows the definition of the
        configuration class. I want to move that class to an independent dll as
        I said above.

        public Configuration Deserialize()
        {
        XmlSerializer serializer =
        new XmlSerializer(t ypeof(Configura tion));
        FileStream stream = new FileStream
        (fileName_,File Mode.Open, FileAccess.Read );
        Configuration configuration =
        (Configuration) serializer.Dese rialize(stream) ;
        stream.Close();
        return configuration;
        }

        public void Serialize(Confi guration config)
        {
        Configuration cfgAsReadFromFi le =
        this.Deserializ e(typeof(Config ));
        if (!config.IsEqua l(cfgAsReadFrom File))
        ...
        XmlSerializer writeSerializer =
        new XmlSerializer( typeof( Configuration ) );
        MemoryStream memoryStream = new MemoryStream();
        writeSerializer .Serialize( memoryStream, config );
        ...
        }


        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: how to create instance after passing class type to function

          micha <nospam@devex.c om> wrote:[color=blue]
          > I'll try to be much more specific:
          > next 2 functions belong to class which knows the definition of the
          > configuration class. I want to move that class to an independent dll as
          > I said above.[/color]

          In that case, you'll need to change the signature of both Serialize and
          Deserialize to be "object" (or some interface). At that point, you
          don't need to cast, so you can just return the object after calling
          Deserialize. Instead of using typeof(Configur ation), change the
          signature of the methods to take a parameter which specifies the type.

          Alternatively, you could use generics and make a Deserializer<T> or the
          like.

          --
          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

          • Michael Lev

            #6
            Re: how to create instance after passing class type to function

            Jon,
            Thank you very much.
            It works.

            *** Sent via Developersdex http://www.developersdex.com ***

            Comment

            Working...