Unable to cast object

S

shapper

Hello,

I have the following classes:
public class AssetEdit : AssetForm { }
public class AssetForm : PageBase, IAsset { }

And the validation factory as follows:

public class ValidatorFactory : IValidatorFactory {
private static Dictionary<Type, IValidator> _validators = new
Dictionary<Type, IValidator>();
static ValidatorFactory() {
_validators.Add(typeof(AssetEdit), new AssetFormValidator());
_validators.Add(typeof(AssetCreate), new AssetFormValidator());
} // ValidatorFactory

public IValidator<T> GetValidator<T>() {
return (IValidator<T>)GetValidator(typeof(T));
} // GetValidator

public IValidator GetValidator(Type type) {
IValidator validator;
if (_validators.TryGetValue(type, out validator))
return validator;
return null;
} // GetValidator

} // ValidatorFactory

AssetFormValidator is as follows:
public class AssetFormValidator : AbstractValidator<AssetForm> { }

And on my code I am doing the following:
IValidator v = _validatorFactory.GetValidator<AssetEdit>();

I get the error:
Unable to cast object of type 'Validators.AssetFormValidator' to type
'FluentValidation.IValidator`1[Models.AssetEdit]'.

On code line:
return (IValidator<T>)GetValidator(typeof(T));

What am I doing wrong?

Basically, I am trying to have both models AssetEdit and AssetCreate
to use the same validator AssetFormValidator.
Of course later I can create different validators for both necessary.

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
[...]
AssetFormValidator is as follows:
public class AssetFormValidator : AbstractValidator<AssetForm> { }

And on my code I am doing the following:
IValidator v = _validatorFactory.GetValidator<AssetEdit>();

I get the error:
Unable to cast object of type 'Validators.AssetFormValidator' to type
'FluentValidation.IValidator`1[Models.AssetEdit]'.

On code line:
return (IValidator<T>)GetValidator(typeof(T));

What am I doing wrong?

The primary thing you've done wrong is failed to post a
concise-but-complete code example that reliably reproduces the problem.
Worse, you haven't even posted enough code for even the little bit you
posted to be understandable, because entire type declarations have been
left out.

Now, that said...based on the error, it seems obvious that you are
trying to return an IValidator<AssetEdit>, but instead you've got the
class AssetFormValidator. Since the latter doesn't implement
IValidator<AssetEdit>, the cast fails.

Probably you need to make your AssetFormValidator class generic, so that
it can implement the generic interface IValidator<T>, and then
instantiate the proper parameterized AssetFormValidator<T> when you
initialize your factory's static dictionary (e.g. for AssetEdit, create
an AssetFormValidator<AssetEdit>). It doesn't matter if the class never
uses the type parameter; you just need to implement the generic
interface that goes with the type, even if that implementation never
takes advantage of the specific type.

Now, in spite of the fact that (with your help :p) I've had a fair
amount of practice in psychic debugging, there is still the possibility
that the above guess has nothing to do with the problem you're having.
If so, you really need to post a question that is more complete.
Psychic debugging can only take you so far.

Pete
 
J

Jeroen Mostert

shapper said:
I have the following classes:
public class AssetEdit : AssetForm { }
public class AssetForm : PageBase, IAsset { }
public IValidator<T> GetValidator<T>() {
return (IValidator<T>)GetValidator(typeof(T));
} // GetValidator

AssetFormValidator is as follows:
public class AssetFormValidator : AbstractValidator<AssetForm> { }
which matters quite a said:
And on my code I am doing the following:
IValidator v = _validatorFactory.GetValidator<AssetEdit>();

I get the error:
Unable to cast object of type 'Validators.AssetFormValidator' to type
'FluentValidation.IValidator`1[Models.AssetEdit]'.

On code line:
return (IValidator<T>)GetValidator(typeof(T));

What am I doing wrong?
AssetFormValidator (presumably) implements IValidator<AssetForm>, not
IValidator<AssetEdit>. Interfaces in C# 3 do not support variance:
IMyInterface<B> is not compatible with IMyInterface<A>, even if B inherits
from A (or the other way around, like in this case), because this would
violate type safety in general. C# 4 will support this scenario (both ways)
through explicit declarations with restrictions on how the interface members
can look. See http://msdn.microsoft.com/library/dd997386(VS.100)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top