By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,333 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,333 IT Pros & Developers. It's quick & easy.

Passing generic type

P: n/a
Hi,
I am trying to make a generic method which takes as one of its
parameters, a Type. So, i have:

using System; using System.IO; using System.Xml.Serialization;

CreateXml(object o, Type typeOfObj, string filename)
{
... stream declaration..
... xmlserializer declaration

serialXml = new XmlSerializer(typeOfObj);
streamW = new StreamWriter(filename, false);
serialXml.Serialize(streamW, (typeOfObj) o);
....
}

The compiler complains about the last line there where there is a case
of object 0, onto the typeOfObj. What is the correct way to accomplish
this? The compiler message is: the type or namespace 'typeOfObj' coud
not be found (are you missing a directive or an assembly reference?)
thanks,
BRAMOIN

*** Sent via Developersdex http://www.developersdex.com ***
Apr 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Meya-awe wrote:
I am trying to make a generic method which takes as one of its
parameters, a Type. So, i have:

using System; using System.IO; using System.Xml.Serialization;

CreateXml(object o, Type typeOfObj, string filename)
{
... stream declaration..
... xmlserializer declaration

serialXml = new XmlSerializer(typeOfObj);
streamW = new StreamWriter(filename, false);
serialXml.Serialize(streamW, (typeOfObj) o);
....
}

The compiler complains about the last line there where there is a case
of object 0, onto the typeOfObj. What is the correct way to accomplish
this? The compiler message is: the type or namespace 'typeOfObj' coud
not be found (are you missing a directive or an assembly reference?)


What would you expect the cast to do anyway? The signature for
XmlSerializer.Serialize just has "object" as the second parameter, so
you don't need a cast.

Jon

Apr 5 '06 #2

P: n/a
thanks!
We forget the obvious. I returned an object and then cast it to the
right type. Sometimes, I try to be over-inventive :-).

BRAMOIN

*** Sent via Developersdex http://www.developersdex.com ***
Apr 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.