Click here to Skip to main content
15,942,449 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an interface:
C#
namespace IVR.VoiceElementsEngine
{
    public interface IVoiceResource
    {
        void ClearDigitBuffer();
    }
}

And the implementation:
C#
namespace IVR.VoiceElementsEngine
{
    public class VoiceElementsResource:IVoiceResource
    {
        private readonly VoiceResource _voiceResource;
        public VoiceElementsResource(VoiceResource voiceResource)
        {
            _voiceResource = voiceResource;
        }
        
        public void ClearDigitBuffer()
        {
            _voiceResource.ClearDigitBuffer = true;
        }
    }
}


Now in another class, I have
C#
namespace IVR.VoiceElementsEngine.CallObjects
{
    public class VoiceElementsLine : ILine
    {
        public IVoiceResource IncomingVoiceResource { get; set; }
        
        public VoiceElementsLine()
        {
            
        }
        public VoiceElementsLine(IVoiceResource voiceElementsResource):this()
        {
            IncomingVoiceResource = voiceElementsResource;
        }
    }
}

In my unit test with Moq, I have:
C#
namespace IVR.Tests
{
    public class VoiceElementsLineTest
    {
        [Fact]
        public void PlayPromptTextToSpeech_Should_Play_A_Text()
        {
            // ARRANGE
            var voiceResourceMock = new Mock<IVoiceResource>();
            var voiceResource = voiceResourceMock.Object;
            var line = new VoiceElementsLine(voiceResource);

I got the error:
Error CS1503 Argument 1: cannot convert from 'VoiceElements.Interface.IVoiceResource' to 'IVR.VoiceElementsEngine.IVoiceResource'
I guess that it is "cast an interface to a concrete implementation" issue. How to fix it?
Posted
Comments
Sergey Alexandrovich Kryukov 3-Nov-15 9:07am    
In what line?
No, this is casting an interface to an interface. Fix what? It depends on what you wanted to achieve.
—SA
[no name] 3-Nov-15 9:14am    
The last line: var line = new VoiceElementsLine(voiceResource);

1 solution

It looks like you have two versions of the interface. One in VoiceElements.Interface and another in IVR.VoiceElementsEngine. To be able to cast you must implement the same instance of the interface.

I had a similar problem when I was loading a single interface from a loaded assembly as well as referencing it directly. This did not work because they were not the same instance.

You must have a version of the interface that both classes implement.
 
Share this answer
 
Comments
[no name] 3-Nov-15 9:25am    
Thanks, it does have two interfaces with the same name. I just renamed one.
Andy Lanng 3-Nov-15 9:26am    
Yeah, I've done that before too ^_^
Maciej Los 3-Nov-15 14:51pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900