Lakmal
Lakmal

Reputation: 918

Access dependencies in extension methods

enter image description here

How can I access the factory property in MediaMuxer inside this MergeChannels extension method? is it possible?

public class MediaMuxer : IMediaMuxer
{
   protected readonly IProcessWorkerFactory factory;
   public MediaMuxer(IProcessWorkerFactory processFactory);
}

Upvotes: 0

Views: 93

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500625

No, you can't access the factory field, because it's protected - it would only be accessible from a subclass of MediaMuxer, and extension methods can only be declared in static classes, so those can't be subclasses of MediaMuxer. (And even then, it's only accessible within an instance of the subclass in which the accessing code is written - not just any arbitrary MediaMuxer.)

Additionally, even if that were public, muxer is declared to be of type IMediaMuxer, not MediaMuxer, so you'll only have access to members declared in IMediaMuxer. So basically:

  • If you can make it a public property in IMediaMuxer, that would do it
  • Otherwise, make it a public property (rather than a field, if possible) in MediaMuxer and change your extension method's first parameter to be of type MediaMuxer.

Upvotes: 3

Related Questions