Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Implementing ISerializable and IBindableComponent
 

Implementing ISerializable and IBindableComponent

Hello All,

I’m attempting to create my own class to be used as the cached data island for a word 2003 VSTO project. This class must be Serializable so that the cache manager can store the specific information along with the document, however I’d also like it to be Bindable so that I can drag the property items from the data-source window within visual studio, directly onto the word canvas for inclusion with the document as bookmark controls.

I reached a sticking point as when I attempt to save the document, word attempts to serialize my class which fails with the following error �/span>

“You must implement a default accessor on System.Windows.Forms.BindingContext because it inherits from ICollection.�/span>

I have read the related article written by Kyle, however I’m still struggling with this one.

Any help will be greatly appreciated.

Kind Regards,

Tim


(Visual Studio 2008 using Tools for Office development - Word 2003)

cazpian  Wednesday, July 22, 2009 8:55 AM
Hi cazpian,

The error indicates we need to implement a default accessor on System.Windows.Forms.BindingContext. As we can see from the MSDN document, BindingContext implements the ICollection and the IEnumerable.

I performed a serach, I found that this error is commonly seen when serializing the custom collections. So here, I think you can try to inherit from the BindingContext and implement the the default accessor. You can take a look at this similar thread for knowing why default accessor is needed.

If you have any problem, please feel free to let me know.


Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, July 24, 2009 4:07 AM

[Serializable()]
[Bindable(BindableSupport.No)]
public class LetterStructure:
ISerializable,
ILetter_GasServicing,
ICachedType,
IBindableComponent
{


#region " class constructors "

public LetterStructure()
{
// initialise the class
Initialise();
}

public LetterStructure(SerializationInfo info, StreamingContext context)
{
// initialise the class
Initialise();

//Get the values from stream and assign them to the appropriate properties
this.JobReference = (string)info.GetValue(const_job_reference, typeof(string));
this.JobDateDue = (DateTime)info.GetValue(const_job_date_due, typeof(DateTime));
this.JobDateRecieved = (DateTime)info.GetValue(const_job_reference, typeof(DateTime));
this.ContactAddress = (string)info.GetValue(const_contact_address, typeof(string));
this.ContactFullName = (string)info.GetValue(const_contact_full_name, typeof(string));
this.ContactName = (string)info.GetValue(const_contact_name, typeof(string));

}


#endregion


#region " class variables "

// these are the variables used to hold the referenced data
private string _job_reference;
private DateTime _job_date_due;
private DateTime _job_date_received;
private string _contact_fullname;
private string _contact_address;
private string _contact_name;


// serializable names
private const string const_job_reference = "JobReference";
private const string const_job_date_due = "JobDateDue";
private const string const_job_date_received = "JobDateRecieved";
private const string const_contact_full_name = "ContactFullName";
private const string const_contact_name = "ContactName";
private const string const_contact_address = "ContactAddress";

// create a dictionary object containing the bookmarks
[NonSerialized()]
private ArrayList documentBookmarks;

// change tracking
private Boolean dataIsDirty = false;

// binding attributes
[NonSerialized()]
private BindingContext localBindingContext;
[NonSerialized()]
private ControlBindingsCollection localDataBindings;

#endregion

#region " Iletter Properties "

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
public string JobReference
{
get
{
return _job_reference;
}
set
{
_job_reference = value;
}
}

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
public DateTime JobDateDue
{
get
{
return _job_date_due;
}
set
{
_job_date_due = value;
}
}

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
public DateTime JobDateRecieved
{
get
{
return _job_date_received;
}
set
{
_job_date_received = value;
}
}

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
public string ContactFullName
{
get
{
return _contact_fullname;
}
set
{
_contact_fullname = value;
}
}

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
public string ContactName
{
get
{
return _contact_name;
}
set
{
_contact_name = value;
}
}

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
public string ContactAddress
{
get
{
return _contact_address;
}
set
{
_contact_address = value;
}
}

#endregion

#region " private methods "

private void Initialise()
{
try
{
// create a new instance of the document bookmark objects
documentBookmarks = new ArrayList();


// create and populate the list object with bookmark objects
documentBookmarks.Add(const_job_reference);
documentBookmarks.Add(const_job_date_due);
documentBookmarks.Add(const_job_date_received);
documentBookmarks.Add(const_contact_name);
documentBookmarks.Add(const_contact_full_name);
documentBookmarks.Add(const_contact_address);

} catch (Exception ex)
{
// display the error for debugging and
// pass to next level for handling
Debug.WriteLine(ex.Message);
throw (ex);
}

}

#endregion


#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
//http://www.codeproject.com/KB/cs/objserial.aspx
// add the current values from the properties to the stream
info.AddValue(const_job_reference, typeof(string));
info.AddValue(const_job_date_due, typeof(DateTime));
info.AddValue(const_job_date_received, typeof(DateTime));
info.AddValue(const_contact_full_name, typeof(string));
info.AddValue(const_contact_name, typeof(string));
info.AddValue(const_contact_address, typeof(string));

}

#endregion


#region ICachedType Members

public void AfterLoad()
{
// reset the document modified state
dataIsDirty = false;
Debug.WriteLine("after load");
}

public void AfterSave()
{
// reset the document modified state
dataIsDirty = false;
Debug.WriteLine("after load");
}

public bool BeforeLoad()
{
return true;
}

public bool BeforeSave()
{
return true;
}

public bool IsDirty
{
get
{
return dataIsDirty;
}
}

#endregion

#region IBindableComponent Members


[Browsable(false)]
public BindingContext BindingContext
{
get
{
if (localBindingContext == null)
{
localBindingContext = new BindingContext();
}

return localBindingContext;
}
set
{
localBindingContext = value;
}
}

public ControlBindingsCollection DataBindings
{
get
{
if (localDataBindings == null)
{
localDataBindings = new ControlBindingsCollection(this);
}

return localDataBindings;
}
}

#endregion

#region IComponent Members

public event EventHandler Disposed;

public System.ComponentModel.ISite Site
{
get
{
return null;
}
set
{
}
}

#endregion

#region IDisposable Members

public void Dispose()
{
// dispose of all items
if (documentBookmarks != null)
{
documentBookmarks.Clear();
documentBookmarks = null;
}
}

#endregion
}

cazpian  Wednesday, July 22, 2009 8:58 AM
Hi cazpian,

The error indicates we need to implement a default accessor on System.Windows.Forms.BindingContext. As we can see from the MSDN document, BindingContext implements the ICollection and the IEnumerable.

I performed a serach, I found that this error is commonly seen when serializing the custom collections. So here, I think you can try to inherit from the BindingContext and implement the the default accessor. You can take a look at this similar thread for knowing why default accessor is needed.

If you have any problem, please feel free to let me know.


Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, July 24, 2009 4:07 AM
Hi cazpian,

How's the problem going? If any help is needed, please feel free to let me know.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Monday, July 27, 2009 6:08 AM
I moved away from implementing both of these interfaces as I'm still having other issues with caching of data islands, so moved to a simpler cache class. I have actually put this down to the serializer attempting to serialize the "BindingContext"property, which can be avoided by using [XmlIgnore()] above the property and would likely solve the issue.

Thanks for your support,

Tim
cazpian  Monday, July 27, 2009 7:19 PM

You can use google to search for other answers

Custom Search

More Threads

• Databinding on object help
• fix column width in datagrid
• custom control hosting in DataGridView Header
• DataNavigator and query toolstrip interaction
• DataGridViewComboBoxCell BackColor in Vista
• ItemChanged Event for Windows Form DataGrid ?
• Can not add references to a managed c++ project in visual studio 2005
• TIMEOUT when try to save data from datagrid
• DataGridView Default Error Dialog
• Autocomplete in a combobox of a DataGridview