I'm having a problem with Casting an object to it's implemented Interface, across different assemblies, the Interfaces are also using Generics.
I have 3 libraries. Contracts (Interface Location) Implementations (Interface Implementations) MainAssembly (Happens to be an ASP.NET MVC5 application)
Main references Contracts and Implementations. Implementations references Contracts.
Contracts Contains 2 Interfaces, one uses a generic that is of the other Interface type
public interface IConfig{}
public interface IStore<TConfig> where TConfig : IConfig{}
Implementations Contains 2 Classes that Implements these Interfaces like so.
public class ConfigBase : IConfig {}
public class Store<TConfig> :IStore<TConfig> where TConfig : ConfigBase {}
ASP.NET application Has a Class that Inherits from the ConfigBase class
public class Configuration : ConfigBase {}
Within the Contracts Library, there is a Service Class, that contains a Method that Requires a type of IStore
public void DoSomething(IStore<IConfig> store){}
However Passing in a type of Store is telling me it can't convert it. This is where the compile time error occurs, everything appeasrs to build correctly.
Store<Configuration> store = new Store<Configuration>();
serviceObj.DoSomething(store);
使用让您通用接口协变 out
通用修饰符
public interface IStore<out TConfig> where TConfig : IConfig{}.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.