Cysharp/ObservableCollections

Unable to Cast ObservableList<Class> to IObservableCollection<IReadOnlyClass>

Opened this issue · 0 comments

I am currently facing a type conversion issue where I am unable to cast ObservableList<Class> to IObservableCollection<IReadOnlyClass>. Below is the relevant code snippet that demonstrates the issue:

using ObservableCollections;

public interface IReadOnlyClass
{
    int Value { get; }
}

public class Class : IReadOnlyClass
{
    public int Value { get; set; }
}

public interface IReadOnlyExample
{
    IObservableCollection<IReadOnlyClass> List { get; }
}

public class Example : IReadOnlyExample
{
    private ObservableList<Class> _list;

    // CS0029: Cannot implicitly convert type 'ObservableList<Class>' to 'IObservableCollection<IReadOnlyClass>'
    public IObservableCollection<IReadOnlyClass> List => _list;
}

I am looking for a solution or workaround that would allow this type of conversion, ideally without having to manually create a new collection or extensively modify the existing data structures. Any suggestions or insights would be greatly appreciated.