mardi 15 février 2022

Does C# currently support contravariant interfaces? (E.g. Collections of interfaces without requiring inheritance of a derived interface)

Does C# have a way of requiring a property or field to implement a multiple interfaces without requiring implementation of a derived interface?

As an example of non-working code to explain the concept:

public interface IBase1 {...}
public interface IBase2 {...}
public interface in IContravariantDerived : IBase1,IBase2 {}

public class DerivedClass : IBase1,IBase2 {...}

Ex1:

IContravariantDerived MyObject = new DerivedClass(); //Fails because object does not implement IContravariantDerived 

Ex2:

interface<IBase1,IBase2> MyObject = new DerivedClass(); //Fails, not valid syntax.

This would be for interfacing with 3rd part libraries (e.g. a property with INotifyPropertyChanged and INotifyCollectionChanged),but where the 3rd party library is sealed, or boiler plate wrappers would bloat the code.

If there isn't anything in the current spec, does an IL weaving library or a planned feature for the next c# version exists?

Aucun commentaire:

Enregistrer un commentaire