문제

I'm trying to serialize an object and the following SerializationException is thrown:

Type 'System.Linq.Enumerable+d__71`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' in Assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

Where is this object in my code? How would I find out? The object graph is fairly large.

도움이 되었습니까?

해결책

Try using Reflector and see if you can determine where the anonymous type d__71`1 is being used in your code.

다른 팁

Sounds to me as though you've stored the results of calling an enumerator method without converting the result to a list.

When you have a method like this:

public IEnumerable<string> GetMyWidgetNames()
{
    foreach (var x in MyWidgets)
    { 
        yield return x.Name;
    }
}

The compiler turns this into a nested object with a name like the one you're seeing (one with a name you could never create yourself because of the embedded +)

If you then store a reference to this object inside something you try to serialise, you'll get the exception noted by the OP.

The "fix" is to ensure that your serialised objects always convert any IEnumerable<> assignments into lists. Instead of this

public IEnumerable<string> WidgetNames { get; set; }

you need to write:

public IEnumerable<string> WidgetNames
{
    get { return mWidgetNames; }
    set
    {
        if (value == null)
            mWidgetNames= null
        else mWidgetNames= value.ToList();
    }
}
private List<string> mWidgetNames;

Try to serialize an object (single type) at a time and see when it blows up. You can do this manually, or through reflection.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top