Using XmlAttributeOverrides to override Type in a different assemb

G

Guest

Hi,
I have the following class.

Assembly: A.dll
public class Customers {
public Customer[] customer;
}

public class Customer {
public string name;
public string country;
public object obj;
}

The basic need is to serialize this. But the constraint is.. Customer.Obj
can contain a Type of either "Order" or "Product" at runtime. This actual
type is not known at design time. For this I use XmlAttributeOverrides to
override the Type and Serialize it. However it seems to require that the
definitions for "Order" or "Product" Types be present within the assembly
A.dll itself where the code to serialize is present. But in my case it would
be present in another assembly (say) B.dll.

To make it more clear - A.dll contains the "Customers" type to be serialzed.
The types that Customer.Obj can take at runtime are defined in B.dll. The
code to serialize the "Customers" type by using XmlAttributeOverrides is also
present in A.dll. Now, this does not work as the definitions for the types
that Customer.Obj can take are in B.dll. However if I move definitions for
the types that Customer.Obj
can take inline into A.dll, it works fine.

So, How can I override attributes present in a different dll using
XmlAttributeOverrides.

Thanks and Regards,
Kishore
 
N

Nicholas Paldino [.NET/C# MVP]

Kishore,

It's not that it requires the definition to be in the same dll, but
rather, when you deserialize it, the assembly containing the types have to
be loaded. Because of this, you will want make sure that the assembly B.dll
is loaded (either through a reference, or a call to the static Load method
on the Assembly class).

Hope this helps.
 
N

Nick Malik

Hello Kishore,

Since you decided not to use OO principles in designing your class
heirarchy, the serializer is stumped. It doesn't know what object to look
for.
If both Order or Product can be stored in that object, then either (a)
create an interface that both Order and Product can inherit from or (b)
create a little heirarchy in the b.dll to do encapsulate this.

option (a) is far better. However, I will demonstrate (b) because I don't
know what else is in your order or product classes.

public interface EmbeddedType
{
}

public class OrderContainer : EmbeddedType, Order
{
}

public class ProductContainer: EmbeddedType, Product
{
}

public class Customer {
public string name;
public string country;
public EnbeddedType obj;
}


Now, the serializer should have very little difficulty figuring out what is
included in what.

--- Nick
 
G

Guest

I tried loading the assembly using Assembly.LoadFrom just before the
serialization happens. Still, the same issue persists. I also tried aadding a
reference. Still, the same thing. What could I be doing wrong.

Kishore

Nicholas Paldino said:
Kishore,

It's not that it requires the definition to be in the same dll, but
rather, when you deserialize it, the assembly containing the types have to
be loaded. Because of this, you will want make sure that the assembly B.dll
is loaded (either through a reference, or a call to the static Load method
on the Assembly class).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kishore Gopalan said:
Hi,
I have the following class.

Assembly: A.dll
public class Customers {
public Customer[] customer;
}

public class Customer {
public string name;
public string country;
public object obj;
}

The basic need is to serialize this. But the constraint is.. Customer.Obj
can contain a Type of either "Order" or "Product" at runtime. This actual
type is not known at design time. For this I use XmlAttributeOverrides to
override the Type and Serialize it. However it seems to require that the
definitions for "Order" or "Product" Types be present within the assembly
A.dll itself where the code to serialize is present. But in my case it
would
be present in another assembly (say) B.dll.

To make it more clear - A.dll contains the "Customers" type to be
serialzed.
The types that Customer.Obj can take at runtime are defined in B.dll. The
code to serialize the "Customers" type by using XmlAttributeOverrides is
also
present in A.dll. Now, this does not work as the definitions for the types
that Customer.Obj can take are in B.dll. However if I move definitions
for
the types that Customer.Obj
can take inline into A.dll, it works fine.

So, How can I override attributes present in a different dll using
XmlAttributeOverrides.

Thanks and Regards,
Kishore
 

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

Top