I have a set of classes which all take part in an inheritance chain, listed at the bottom of this post, the last two types and the method following them are part of an abstract library and the first class the usage of such.
The problem I am having is upon instantiation of the UserService type using Activator.CreateInstance – with an InvalidCastException stopping me dead; this did used to cast OK though, so I’m probably just overlooking something that I’ve recently altered subtly (or stupidly).
As detailed in the method creating this instance at runtime, this is the code causing the error:
return (IDataService<DataContextType, DataConnectionProviderType, DataEntityType>)Activator.CreateInstance(service);
However, the following code succeeds:
var a = new UserService();
var b = (UserService<DataContext, ConnectionProvider, User, UserRole>)a;
var c = (DataService<DataContext, ConnectionProvider, User>)b;
var d = (IDataService<DataContext, ConnectionProvider, User>)c;
var e = (IDataService<DataContext, ConnectionProvider, User>)a;
Can anyone see what I can’t?
Not entirely sure what is going on here, but have a hunch it may be related to the typing of IDataEntity. The code for your examination, enquire if you feel you need more:
public class UserService
: UserService<DataContext, ConnectionProvider, User, UserRole>
{
}
public class UserService<DataContextType, DataConnectionProviderType, UserType, UserRoleType>
: DataService<DataContextType, DataConnectionProviderType, UserType>
where DataContextType : DataContext<DataConnectionProviderType>, new()
where DataConnectionProviderType : DataConnectionProviderBase, new()
where UserType : class, IUser<UserRoleType>, IDataEntity
where UserRoleType : class, IUserRole, IDataEntity
{
}
public abstract class DataService<DataContextType, DataConnectionProviderType, DataEntityType>
: IDataService<DataContextType, DataConnectionProviderType, DataEntityType>
where DataContextType : DataContext<DataConnectionProviderType>, new()
where DataConnectionProviderType : DataConnectionProviderBase, new()
where DataEntityType : class, IDataEntity
{
}
public interface IDataService<DataContextType, DataConnectionProviderType, DataEntityType> : IDisposable
where DataContextType : DataContext<DataConnectionProviderType>, new()
where DataConnectionProviderType : DataConnectionProviderBase, new()
where DataEntityType : class, IDataEntity
{
}
public static IDataService<DataContextType, DataConnectionProviderType, DataEntityType>
LoadServiceType<DataContextType, DataConnectionProviderType, DataEntityType>(Type service)
where DataContextType : DataContext<DataConnectionProviderType>, new()
where DataConnectionProviderType : DataConnectionProviderBase, new()
where DataEntityType : class, IDataEntity
{
...
return (IDataService<DataContextType, DataConnectionProviderType, DataEntityType>)Activator.CreateInstance(service);
}
Exception Details:
System.InvalidCastException: Unable to cast object of type '...UserService' to type '...IDataService'3[...DataContext,...Domain.Data.ConnectionProvider,...IDataEntity]'.
It looks like you’re actually calling:
That won’t work, because it’s trying to cast to
when
UserServiceactually implementsYou’ll find that if you change your test code to try to cast using
IDataEntityas a type argument instead ofUser, it’ll fail in the same way.This is all to do with generic variance. It’s a big topic – Eric Lippert has a whole series of blog posts on it and last year I gave a talk at NDC on it… I don’t know if the streaming site with the talk on is still available, but you can grab a torrent of all the talks here.
To give a very simple example, you can’t cast an
IList<string>toIList<object>in exactly the same way – otherwise this would compile, but have to fail at execution time: