Home > other >  No Implicit Reference Conversion
No Implicit Reference Conversion

Time:10-14

I have a public interface which controls if an object which implements it should be visible or not. The basic structure looks like this:

public interface IPreviewObject
{
  bool Hidden { get; set; }
}

Then, I have a class which implements this interface (along with some other interfaces)

public abstract class MyParameter<T> : Param<T>, IParameter where T : class, IPreviewObject
{
  protected MyParameter(string name)
    : base(name)
  {
  }
  private bool m_hidden = false;
  public bool Hidden
  {
    get { return m_hidden; }
    set { m_hidden = value; }
  }
}

Lastly, I have a second class which inherits from the MyParameter class like this:

public sealed class MyComponent : MyParameter<SpecialType>
{
  public MyComponent ()
    : base("My Component Name")
  {
  }
}

However, I get an error on the definition of my second class MyComponent which says There is no implicit reference conversion from SpecialType to IPreviewObject. If I remove the IPreviewObject from the MyParameter definition, then the same code works fine. But, when I try to add the IPreviewObject inheritance, I get this error. Can anyone explain why? Or how to fix this?

CodePudding user response:

SpecialType should implement interface IPreviewObject


public class SpecialType : IPreviewObject
{
    public bool Hidden { get => throw new NotImplementedException(); 
                        set => throw new NotImplementedException(); }
}
  •  Tags:  
  • c#
  • Related