Passing generic type

  • Thread starter Thread starter Meya-awe
  • Start date Start date
M

Meya-awe

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
 
Meya-awe said:
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
 
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
 

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