how to create instance after passing class type to function

  • Thread starter Thread starter mlev
  • Start date Start date
M

mlev

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
 
mlev said:
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,
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.
 
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 );
...
}
 
micha said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top