tags:

views:

271

answers:

7

I have the following code in a method of a class:

XmlSerializer serializer = new XmlSerializer(typeof(FooClass));

How do I pass into the constructor or a parameter of the class - the foo class?

I think it has something to do with reflection as you can't pass as a parameter just a class name - so the following code makes some sort of sense (thanks to Aidan) -

XmlSerializer serializer = new XmlSerializer(Type.GetType("namespace.FooClass"));
A: 

Does this do what you want?

XmlSerializer serializer = new XmlSerializer(foo.GetType());
heavyd
not really - the serializer need to examine the class type
What do you mean it needs to examine the class type? Assuming foo is an instance of the object you want to serialize which is publicly visible, the XmlSerializer will use reflection to "examine" the class.
heavyd
Sorry I'm being a bit crap about what I am trying to ask - I'm not really sure how to phrase it as I still trying to get my head round the problem
No problem, I'm just trying to understand as well. Please edit your original question with any additional details you can add to help clarify what you are trying to achieve.
heavyd
A: 

Add a type parameter to the method:

 public XmlSerializer Method<T>()
 {
    return new XmlSerializer(typeof(T));
 }

you would then call the method as Method<Foo>();

Bryan Rowe
I presume you mean new XmlSerializer(typeof(T)).
Steve Guidi
A: 

Consider this snippet of code:

Type t = foo.GetType();
XmlSerializer fooSerializer = MakeSerializer(t);

...

private XmlSerializer MakeSerializer(Type myType)
{
  XmlSerializer serializer = new XmlSerializer(myType);
  return serializer;
}
p.campbell
What's the added value of the MakeSerializer method ? why not use the XmlSerializer constructor directly ?
Thomas Levesque
@Thomas - good question. It was a contrived example.
p.campbell
+1  A: 
void serializeme(Object o) {
   Type t = o.GetType();
   XmlSerializer serializer = new XmlSerializer(t);
   ...
}
nos
A: 

You would use reflection to achieve what (I believe) you want to do.

You would use the XmlSerializer(Type) override of the XmlSerializer constructor.

XmlSerializer serializer = new XmlSerializer(Type.GetType("namespace.Foo"));

or

XmlSerializer serializer = new XmlSerializer(fooInstance.GetType());

depending if you have an instance of the type on hand.

Aidan
I am getting an exception thrown (Type.GetType("namespace.Foo")) - "Could not load type 'fooNamespace.Bar' from assembly 'FooBar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' - any ideas ?
did you get the namespace and class name right?
Aidan
A: 

As far as I know you have to use a parameterless constructor with the XmlSerializer. So the only constructor of foo that XmlSerializer will use is this:

public foo()
{
}
Jake Pearson
that's *almost* correct... you can actually have an `internal` constructor, if you generate the serialization assembly with sgen.exe, and declare that assembly as friend with `InternalsVisibleToAttribute`
Thomas Levesque
+1  A: 

If I understand your question correctly, you want this:

class YourClass {
   Type type;

   public YourClass(Type type) {
       this.type = type;
   }

   public XmlSerializer Method() {
       return new XmlSerializer(type);
   }
}

...

new YourClass(typeof(Foo));
Pavel Minaev
You are absolutely right - many thanks it worked.