views:

114

answers:

2

So, I'm working with the following assembly, which has the following defined (fairly harmless):

public class QueryDefinition
{
    private List<QueryFilter> TheCurrentFilters = null;

    public List<QueryFilter> CurrentFilters
    {
        set { TheCurrentFilters = value; }
        get { return TheCurrentFilters; }
    }

    // other code

    public class QueryFilter
    {
        // member variables are: seven public string's & two public int's

        public override string ToString()
        {
            return FilterText;
        }
    }
}

Within another assembly, we have a UserControl:

public partial class QueryWizard : UserControl
{
    private List<QueryDefinition.QueryFilter> TheCurrentFilters = null;

    public List<QueryDefinition.QueryFilter> CurrentFilters
    {
        set { TheCurrentFilters = value; }
        get { return TheCurrentFilters; }
    }

    // other code
}

Interesting code, but that's what I have to work with.

Anyhow, if I go to another project (that references this UserControl), create a Form, and then drop the control onto the Form, I get this error:

'System.Runtime.Serialization.SerializationException: Type QueryDefinition+QueryFilter' in Assembly ... is not marked as serializable.'

I'm not actually using any Serialization code, so what of this List of QueryFilter's is the reason for a SerializationException?

I have used the [Serializable] tag, to get rid of this. But recently we were rebuilding projects (Visual WebGUI upgrade) and now I run into the "unable to load type required for deserialization" issue. Instead of figuring out that problem, I decided to try and figure out why we need the Serialization tags in the first place! Thanks.

+2  A: 

It is because the designer tries to serialize the contents of the usercontrols "CurrentFilters" property into the form initialization code.

Check the DesignerSerializationVisibility attribute: http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibility.aspx

If you don't intend to support designtime editing of the CurrentFilters property, setting it to hidden should fix the problem (I think, was ages since I built winforms controls)

Roger Alsing
A timely, and right answer! I appreciate it greatly. Green check-mark for you!
JustLooking
+2  A: 

The actual values for the CurrentFilters are getting serialized using BinaryFormatter and stored in a .resx file. You almost certainly don't want this to happen. For one, you'll take a dependency on the [AssemblyVersion] number of the assembly that contains your QueryFilter class. Which should explain the "unable to load type" exception you get now.

First find out how CurrentFilters ended up with values at design time. You'll need to beware of events that run at design time. The typical candidates are the constructor and the Load event. Use the Control.DesignTime property to prevent code from running.

Next, ensure that the property value doesn't get persisted by applying an attribute:

 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
 public List<QueryFilter> CurrentFilters
 {
 }
Hans Passant
Spot-on. Thanks so much for your time. I upvoted the answer, but I think the green checkmark is going to Roger. I wish I could give it to both of you, but it looks like Roger needs it a bit more. You've got enough points for a lifetime!
JustLooking