467,081 Members | 961 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

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(Type cfgType)
{
myType obj = Activator.CreateInstance(cfgType);
XmlSerializer serializer = new XmlSerializer( cfgType);
FileStream stream = new FileStream("some file...", FileMode.Open,
FileAccess.Read);
cfgType configuration = (cfgType)serializer.Deserialize(stream);
}
thanks

Dec 26 '05 #1
  • viewed: 1752
Share:
5 Replies
mlev <ml**@nds.com> wrote:
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(Type cfgType)
{
myType obj = Activator.CreateInstance(cfgType);
XmlSerializer serializer = new XmlSerializer( cfgType);
FileStream stream = new FileStream("some file...", FileMode.Open,
FileAccess.Read);
cfgType configuration = (cfgType)serializer.Deserialize(stream);
}


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 27 '05 #2
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 ***
Dec 27 '05 #3
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(typeof(Configuration));
FileStream stream = new FileStream
(fileName_,FileMode.Open, FileAccess.Read);
Configuration configuration =
(Configuration)serializer.Deserialize(stream);
stream.Close();
return configuration;
}

public void Serialize(Configuration config)
{
Configuration cfgAsReadFromFile =
this.Deserialize(typeof(Config));
if (!config.IsEqual(cfgAsReadFromFile))
...
XmlSerializer writeSerializer =
new XmlSerializer( typeof( Configuration ) );
MemoryStream memoryStream = new MemoryStream();
writeSerializer.Serialize( memoryStream, config );
...
}
*** Sent via Developersdex http://www.developersdex.com ***
Dec 27 '05 #4
micha <no****@devex.com> wrote:
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.


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(Configuration), 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 29 '05 #5
Jon,
Thank you very much.
It works.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by etuncer@gmail.com | last post: by
45 posts views Thread by =?Utf-8?B?QmV0aA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.