简体   繁体   中英

Implicit conversion from type to interface in C# — Basic example works, but actual implementation has a compile-time error

Take the following simple example:

interface IVehicle {
}

class Car : IVehicle {
}

Now I should be able to do the following:

IVehicle vehicle = new Car();

And indeed if I create these base classes as above, I receive no compilation errors (and the code runs.)

But in my project wherever I'm doing something essentially like that (as far as I can see) I get the following error:

Cannot implicitly convert type 'Namespace.Path.Car' to 'IVehicle'. An explicit conversion exists (are you missing a cast?)

Sometimes (as in different code locations, not different compiles) the explicit cast works and sometimes it compiles but creates a runtime exception (invalid cast). Why does the basic example work without issue, and a more detailed class (but still a single class that directly implements the interface) has all these issues?

In some situations, it'll actually come up with this compile-time error:

Cannot implicitly convert type 'Namespace.Path.IVehicle' to 'IVehicle'. An explicit conversion exists (are you missing a cast?)

I think I'm missing some important condition that is required for this implicit cast to work, but I have not found what difference causes the simple example to work and the more detailed class to fail so strangely. The only thing I've seen of note is that the compile time error only includes the namespace on the first type (as shown in the above two errors), but I can't remember if that is normal or not.

To remove any environmental causes, I created a test case with just the following code:

ISimpleInterface simple = new SimpleImplementation();
IComplexInterface complex = new ComplexImplementation();

The above causes a compile time error on the second line (indicating it cannot do the implicit conversion).

You have either two types named IComplexInterface or two types named ComplexImplementation in different namespaces.

Remove using statements and use full qualifiers for the types and see if it resolves the problem. Navigate to the implementing class double check so that it implements the same interface as you are assigning to. Pay careful attention to to what namespace the the interface and class lies in.

Indirect interface instantiation is normal. As defined for brevity in your question, the example will compile and run.

public interface ICar{}
public class Viper : ICar{}

ICar fun = new Viper();

Pretty basic stuff, although usually this will be wrapped in some sort of Factory pattern. Either way, the process is common and therefore the problem must lie in your implementation.

Found out what's causing it. This interface is being used as the WCF service contract. Svcutil is apparently creating a duplicate of the interface in the global namespace. Since they were in different namespaces, there was no compilation error. The part about the error indicating two different names (one with the namespace, one without) was an indicator that this was causing it.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM