GraphDiff icon indicating copy to clipboard operation
GraphDiff copied to clipboard

OwnedCollection crashes for custom collection types

Open Jerther opened this issue 9 years ago • 5 comments

When calling OwnedCollection on a custom collection, I get the following error:

GraphDiff requires the collection to be either IEnumerable<T> or T[]

The custom collection is something like:

class FooCollection : List<Foo>

The error seems to from this code:

CollectionGraphNode
private Type GetCollectionElementType()
{
    if (Accessor.PropertyType.IsArray)
    {
        return Accessor.PropertyType.GetElementType();
    }

    if (Accessor.PropertyType.IsGenericType)
    {
        return Accessor.PropertyType.GetGenericArguments()[0];
    }

    throw new InvalidOperationException("GraphDiff requires the collection to be either IEnumerable<T> or T[]");
}

Is there a workaround for this?

Jerther avatar Jan 13 '16 20:01 Jerther

For now I've figured I could declare my class like so as a workaround:

class FooCollection<T> : List<Foo>
  where T : Foo

But I think we'll agree that we'd rather not do that... :)

Jerther avatar Jan 13 '16 21:01 Jerther

Just modify the source code and add:

if (Accessor.PropertyType.BaseType.IsGenericType)
{
    return Accessor.PropertyType.BaseType.GetGenericArguments()[0];
}

throw new InvalidOperationException("GraphDiff requires the collection to be either IEnumerable<T> or T[] or derived from GenericType");

I've added simple test for this and others were still green. Also it worked on my DB.

kotylo avatar May 26 '16 12:05 kotylo

Thanks!!

I know you stopped supporting the project, but is there any chance for this to make it to the NuGet package?

Jerther avatar May 26 '16 12:05 Jerther

@kotylo Thank you! Could you add a pull request with this modification and test, please?

Dvornik avatar May 26 '16 13:05 Dvornik

@Dvornik, I just checked this on my collection. The fix may not be that simple, but it works for my simple entities. Created pull request here https://github.com/refactorthis/GraphDiff/pull/161

kotylo avatar May 26 '16 14:05 kotylo