473,569 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Dec 26 '05 #1
5 1982
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(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);
}


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.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
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(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 ***
Dec 27 '05 #4
micha <no****@devex.c om> 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(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 - <sk***@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
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
3175
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 below), but is not ideal. function Client() { } Client.prototype.fullname = "John Smith"; var s = "Client"; eval("var o = new " + s + "();"); alert(o.fullname);
7
7898
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 create it's instance in a function named CreateObjInstance. And the function not only generate just CClass01. So I must sent the class type(whit it's namespace) into the function. There has two...
6
3939
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 tell me how to do this. I have a class, inside I have some public functions and private variables. Inside the class I also have a declaration of a new form object. One of the functions of the class...
4
1636
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 appropriate cDerivedX Class ... The following shows a scenario where it was determined that cDerivedA is needed based on a configuration file. Dim lInstance As New cBase()
8
1633
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 query in several different ways: list, xml, dictionary, etc. I was wondering if I can use decorators to accomplish this. For instance, I have the following method def getUsers(self, params):...
4
12422
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 data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
4
6887
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 function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in...
9
3444
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
2981
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 class's constructor, like this: Friend Class clsData Private m_objSQLClient As clsSQLClient Private m_objUsers As clsUsers
0
7704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7620
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7684
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7985
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5234
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3666
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
959
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.