If i have objects with properties of type object or objects that are generics, how can i serialize this?
Eg.
public class MyClass
{
public Object Item { get; set; }
}
or
public class MyClass<T>
{
public T Item { get; set; }
}
EDIT
My Generic class now looks like this:
public class MyClass<T>
{
public MySubClass<T> SubClass { get; set; }
}
public class MySubClass<T>
{
public T Item { get; set; }
}
Additonal question: How can i change the element name for Item at runtime to typeof(T).Name?
Have you tried the
[Serializable]attribute?Although the generic class is only serializable if the generic type parameter is serializable as well.
Afaik there is no way to constrain the type parameter to be serializable. But you can check at runtime using a static constructor: