473,569 Members | 2,489 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 1981
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
3174
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
7618
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
7926
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. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
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
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.