472,328 Members | 1,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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
5 1909
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see...
7
by: Steven.Xu | last post by:
Hello everyone! I have a problem about create an instance automatically in C#. Now I have a class: namespace1.namespace11.CClass01. I want to...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please...
4
by: Joe HM | last post by:
Hello - I have a Base Class where I want a New() implemented that can be called from the outside. This New() should create an instance of the...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my...
9
by: Grant Schenck | last post by:
Hello, I have a base class with several derived classes: ScriptBase + ScriptCallInbound + ScriptCallOutbound I then have another class: Line
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.