1

Those are from Application Insights Version=2.6.4.0

Here is an interface :

public interface ISupportSampling
{
    double? SamplingPercentage { get; set; }
}

And here is what I can see from VS a class that is supposed to implement the interface.

#region Assembly Microsoft.ApplicationInsights, Version=2.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// nsame namespace as the above interface
public sealed class EventTelemetry : ITelemetry, ISupportProperties, ISupportSampling, ISupportMetrics
{
    public EventTelemetry();
    public EventTelemetry(string name);

    public DateTimeOffset Timestamp { get; set; }
    public string Sequence { get; set; }
    public TelemetryContext Context { get; }
    public string Name { get; set; }
    public IDictionary<string, double> Metrics { get; }
    public IDictionary<string, string> Properties { get; }

    public ITelemetry DeepClone();
}

You can see that the only property from the interface is not in the list of public properties.


So, it means that, when coding, 2 things happen that are not desirable :

1)

This code

EventTelemetry eventTelemetry;
// ...
eventTelemetry.SamplingPercentage = 100;

Causes the following compilation error :

'EventTelemetry' does not contain a definition for 'SamplingPercentage' and no accessible extension method 'SamplingPercentage' accepting a first argument of type 'EventTelemetry' could be found (are you missing a using directive or an assembly reference?`

2)

Also, the SamplingPercentage property will not show up in autocomplete.


Why ? Am I being lied to by VS on what I see from the class ? (that SamplingPercentage is indeed present and public, but for some reasons not shown to me ?)

My current workaround (and also given in the last snippet in this doc from Microsoft here ) is to cast as this :

((ISupportSampling) eventTelemetry).SamplingPercentage = 100;

Is there a more "developper friendly" way, without using explicit cast ? (using Visual Studio 2017)

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • 2
    No, because the *implementer* of the class chooses whether to implement interfaces explicitly or implicitly. `EventTelemetry` only implements `SamplingPercentage` when it's being treated as an `ISupportSampling`. – Damien_The_Unbeliever Sep 24 '19 at 10:26
  • @Damien_The_Unbeliever When you say "explicitly or implicitly", you mean by writing `double ? ISupportSampling.SamplingPercentage { get; set; }` (explicit) versus `public double? SamplingPercentage { get; set; }` ? – Pac0 Sep 24 '19 at 10:28
  • 1
    Yes, exactly. That's what they've apparently done here. – Damien_The_Unbeliever Sep 24 '19 at 10:29
  • ah it's starting to make sense, thanks. You can make this an answer. – Pac0 Sep 24 '19 at 10:31
  • There is a [thread](https://stackoverflow.com/questions/143405/c-sharp-interfaces-implicit-implementation-versus-explicit-implementation) about explicit and implicit interfaces – Pavel Anikhouski Sep 24 '19 at 10:31
  • from [this answer](https://stackoverflow.com/a/143425/479251) in target duplicate : "*If you implement an interface implicitly then your class now exposes new behaviours that might only be relevant to a client of the interface and it means you aren't keeping your classes succinct enough (my opinion)*" – Pac0 Sep 24 '19 at 10:35
  • 1
    You're being misled by the decompiled class definition, it only shows public members. An explicitly implemented interface uses private members. Two basic reasons to do this, one is to resolve an ambiguity between interfaces having the same member name (not the case here), the other is to intentionally hide the property because the class already takes care of it. – Hans Passant Sep 24 '19 at 10:41

0 Answers0