Reputation: 3834
Here is a case for which cloning of object fails:
[Serializable]
public class MasterClass
{
public MasterClass(DataRow row)
{
EntityData = row;
}
public DataRow EntityData
{
get;
set;
}
}
for cloning I am using extention method(Clone()
) from this SO question:
while cloning MasterClass
object following error message thrown at runtime:
Type 'System.Data.DataRow' in Assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
Any solution how to handle this?
Upvotes: 0
Views: 118
Reputation: 3643
Try adding a parameterless constructor:
public MasterClass()
{
}
Converting DataRow
Assuming you DataRow
has a Table
:
[Serializable]
public class MyKeyValue {
public string Key { get; set; }
public string Value { get; set; }
}
[Serializable]
public class MasterClass
{
public MasterClass() {}
public MasterClass(DataRow row)
{
var list = new List<MyKeyValue>();
foreach (DataColumn col in row.Table.Columns)
{
list.Add(new MyKeyValue{Key = col.ColumnName, Value = Convert.ToString(row[col.ColumnName])});
}
EntityData = list;
}
public IEnumerable<MyKeyValue> EntityData
{
get;
set;
}
}
Upvotes: 2
Reputation: 6082
You must implement ISerializable interface - for serialization, and add constructor that takes (SerializationInfo info, StreamingContext context) - for deserialization.
Upvotes: 1
Reputation: 1587
Depending on what you are using the DataRow for, you will likely have to parse the values into a custom class that is serializable.
Upvotes: 2