Open wilhen01 opened 10 years ago
Out of interest, does making it not abstract work?
@jchannon Not sure, will try and find time to test that.
@jchannon Just did some testing, and yes, if you remove the abstract modifier from the class the blacklist works correctly. That's not a great workaround though!
I'm running into the same issue deserializing a collection of interfaces using with Json.NET. Having created a JsonConverter
to deserialize to an implementation of the interface I still get an error System.MissingMethodException: Cannot create an instance of an interface.
.
Looking at the source, it seems the problem is caused by;
private static void HandleReferenceTypeCollectionElement(BindingContext bindingContext, IList model, int count, object o)
{
// If the instance specified in the binder contains the n-th element use that otherwise make a new one.
object genericTypeInstance;
if (model.Count > count)
{
genericTypeInstance = model[count];
}
else
{
genericTypeInstance = Activator.CreateInstance(bindingContext.GenericType);
model.Add(genericTypeInstance);
}
...
HandleReferenceTypeCollectionElement
attempts to create a new instance of the generic collection type before copying valid properties from the original deserialized object (the o
parameter) into it. So will always fail for generic collections whose generic type is either abstract or an interface.
In the end I created a custom implementation an IModelBinder
to handle the deserialization of the collection.
I'm having this issue as well :( Even when I blacklist the property, it still fails to bind
Binding to an abstract class??? Stop that. We don't create proxy objects for binding to.
I'm binding to a concrete class which has a property with an abstract type as it's type. I'm trying to ignore this property, but it doesn't work.
@phillip-haydon I think my original post illustrates a perfectly legitimate example. As pointed out by @luisrudge we're actually trying to ignore the abstract type, not bind to it. Blacklisting properties shouldn't be dependent on their modifiers.
@wilhen01 ok, I understand now, I was in hospital reading through my Nancy emails so I didn't fully understand the initial problem.
This MAY be solved when #2061 is complete.
I have a resource type which contains a heterogenous collection of activities, where activities all inherit from a base abstract class. For illustrative purposes, something like this:
When using this.Bind<>() I get an exception:
The same exception still gets thrown using blacklisting, either by string or lambda
I think this must be a bug, unless I'm missing something? I'm using Nancy 0.23.