I have two classes: Lookup
and LookupItem
that Lookup
has a member named Items
that is a collection of LookupItem
s. I'm not able to serialize Lookup
or LookupItem
. With the first I get error The type LookupItem was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
and with the second I got error A circular reference was detected while serializing an object of type Lookup.
.
How can I solve this problem?
I use following code for serialization:
public static string Serialize(object obj)
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
ser.Serialize(writer, obj);
return sb.ToString();
}
UPDATE: skeleton of classes:
[ActiveRecord(Lazy = true)] public class Lookup : ActiveRecordExtender, IComparable { public Lookup() { }
[Property]
public virtual string Title { set; get; }
// creating relation
[HasMany(typeof(LookupItem), Cascade = ManyRelationCascadeEnum.All)]
public virtual IList Items { set; get; }
}
[ActiveRecord(Lazy = true)] public class LookupItem : ActiveRecordExtender { public LookupItem() { }
//creating relation
[BelongsTo("Lookup_ID")]
public virtual Lookup ContainerLookup { set; get; }
[Property]
public virtual string Title { set; get; }
[Property]
public virtual string Value { set; get; }
[Property]
public virtual int SortOrder { set; get; }
}
Please note I'm using Catle ActiveRecord as my ORM and please notice this problem is not related to inheritance from ActiveRecordBase
. Because other classes in this domain that has no relations work properly.