Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

interface error

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
The error I am getting is: Cannot serialize member Base.ClassLibrary.Common.Objects.ListObjects.LobjList of type Base.ClassLibrary.Interfaces.Common.Objects.IDropDownItem because it is an interface. I cannot pinpoint where the error is being thrown in the user control even though I put try catch blocks around all the events and methods that I've created in the user control.

Any suggestions on how I can trap this error? Thanks.
 
remove the try/catch and let the exception bubble up. when the error is thrown log the stacktrace. this will tell you where the error is thrown.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I couldn't get any clues from the stack trace. Here it is:

Code:
[NotSupportedException: Cannot serialize member Base.ClassLibrary.Common.Objects.ListObjects.LobjList of type Base.ClassLibrary.Interfaces.Common.Objects.IDropDownItem because it is an interface.]

[InvalidOperationException: Cannot serialize member ‘Base.ClassLibrary.Common.Objects.ListObjects.LobjList' of type 'System.Collections.Generic.List`1[[Base.ClassLibrary.Interfaces.Common.Objects.IDropDownItem, Base.ClassLibrary, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.]
   System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +925156
   System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +69
   System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +127
   System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo) +118
   System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a) +1824
   System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel) +734

[InvalidOperationException: There was an error reflecting type Base.ClassLibrary.Common.Objects.ListObjects'.]
   System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel) +1638
   System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace) +115
   System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) +76
   System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) +320
   System.Xml.Serialization.XmlSerializer..ctor(Type type) +6
   System.Configuration.SettingsPropertyValue.ConvertObjectToString(Object propValue, Type type, SettingsSerializeAs serializeAs, Boolean throwOnError) +524
   System.Configuration.SettingsPropertyValue.SerializePropertyValue() +91
   System.Configuration.SettingsPropertyValue.get_SerializedValue() +19
   System.Web.Profile.ProfileModule.PrepareDataForSaving(String& allNames, String& allValues, Byte[]& buf, Boolean binarySupported, SettingsPropertyValueCollection properties, Boolean userIsAuthenticated) +1207
   System.Web.Profile.SqlProfileProvider.SetPropertyValues(SettingsContext sc, SettingsPropertyValueCollection properties) +221
   System.Configuration.SettingsBase.SaveCore() +379
   System.Configuration.SettingsBase.Save() +77
   System.Web.Profile.ProfileBase.SaveWithAssert() +31
   System.Web.Profile.ProfileBase.Save() +63
   System.Web.Profile.ProfileModule.OnLeave(Object source, EventArgs eventArgs) +2397611
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
 
somewhere in the system you are trying to serialize an interface, which cannot happen. you need to serialize the implementation.

there may be a serializable attribute on the interface, which must be removed or you have a call to serialize to type IDropDownItem which also cannot happen, you need to serialize to the concrete type.

it could also be the error is with
List<IDropDownItem> as it's a list of interfaces, not List<Implementation of IDropDownList> which would be a concrete object.

instead of the interface IDropDownItem. I would have an immutable Lookup dto object which can be serialized.
Code:
[Serializable]
public class LookupItem
{
   public string Text {get; private set;}
   public string Value {get; private set;}

   public class LookupItem(string text, string value)
   {
      Text = text;
      Value = value;
   }
}
now you are serializing List<LookupItem>.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
better yet, since all you really need to serialize is the data, dump the list to a simple array and serialize the array.
Code:
LookupItem[] items = new List<LookupItem>().ToArray()
Serialize(items);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top