How can an interface publish a public property / method that does not exist in the implementation class?

Question in general: public class DbContext: IDisposable, IObjectContextAdapter

DbContext implements IObjectContextAdapter. IObjectContextAdapter has one property,

public interface IObjectContextAdapter
{
    // Summary:
    //     Gets the object context.
    ObjectContext ObjectContext { get; }
}

however, I cannot find this property in the DbContext; it’s just not in the metadata. The only way to access it is to distinguish DbContext as an IObjectContextAdapter.

I don’t understand - I would think that the public properties of an interface are revealed by the implementing classes, regardless of whether they are poured into the interface or not. I feel like I'm missing something here ...

+4
source share
2

, DbContext :

public class DbContext : IObjectContextAdapter
{
    ObjectContext IObjectContextAdapter.ObjectContext { get { ... }}
}

, , .

. , IEnumerable<T> :

  • IEnumerator GetEnumerator from IEnumerable
  • IEnumerator<T> GetEnumerator IEnumerable<T>

:

public class X : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

.NET Framework , . ConcurrentQueue<T> IProducerConsumerCollection.TryAdd ConcurrentQueue<T>.Enqueue .

. MSDN

+4

, , - , .

interface IExplicit
{
    void Explicit();
}

class Something : IExplicit
{
    void IExplicit.Explicit()
    {
    }
}

new Something(), IExplicit .

var something = new Something();

// Compile time error.
something.Explicit();

// But we can do.
((IExplicit)something).Explicit();
+1

Source: https://habr.com/ru/post/1530212/


All Articles