简体   繁体   中英

VB.net need to Override in parameter from C# library

I am trying to figure out how to inherit from the C# Sampler class from OpenTelemetry.Trace into VB.net class.

When I use Visual Studio to auto implement the abstract class it provides the following code below, however I can't find "IsReadOnlyAttribute" in any dll for any .net framework 4.6.2 which is what our project is using.

Searching online finds me https://learn.microsoft.com/en-us/do.net/api/system.runtime.compilerservices.isreadonlyattribute.-ctor?view.net-7.0&viewFallbackFrom.netframework-4.6.2 which shows that this attribute exists in "System.Runtime.CompilerServices" which is available on in 4.7.1, 4.7.2, 4.8.

Is there another way to make this work or do I need to upgrade our project?

Cheers

Imports OpenTelemetry.Trace
Public Class ConfigurableSampler
  Inherits Sampler

  Public Overrides Function ShouldSample(<IsReadOnlyAttribute> ByRef samplingParameters As SamplingParameters) As SamplingResult
    Throw New NotImplementedException()
  End Function 
End Class

So Today I updated to.Net framework 4.8 but I still get an error in VB在此处输入图像描述

To find out if this was to do with the parameters or the "in" modifier I added a function as per below which shows that this does work and the issue is still with the "in" parameter modifier.

Public Function Test(ByRef samplingParameters As SamplingParameters) As SamplingResult
    Return New SamplingResult(True)
End Function

I am wondering if this a defect of C# to VB or some limitation I am hitting.

The best path forward that I found here was to create the following class in a C# project and then reference this in the VB.net project.

  public abstract class SamplerAdapter : Sampler
  {
    public override SamplingResult ShouldSample(in SamplingParameters samplingParameters)
    {
      return ShouldSampleAdapter(samplingParameters);
    }

    public abstract SamplingResult ShouldSampleAdapter(SamplingParameters samplingParameters);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM