GraphDiff
GraphDiff copied to clipboard
OwnedCollection crashes for custom collection types
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?
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... :)
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.
Thanks!!
I know you stopped supporting the project, but is there any chance for this to make it to the NuGet package?
@kotylo Thank you! Could you add a pull request with this modification and test, please?
@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