Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:50:46+00:00 2026-05-28T07:50:46+00:00

My apologies if it is quite confusing. I have this two main interfaces. First

  • 0

My apologies if it is quite confusing.

I have this two main interfaces. First the ISensor interface:

public interface ISensor<TReading>
    where TReading : ISensorReading<ISensor<TReading>>
{
    event SensorReadingCompletedEH<ISensor<TReading>, TReading> ReadCompleted;
    TReading Read();
}

And the second interface: ISensorReading:

public interface ISensorReading<TSensor>
    where TSensor : ISensor<ISensorReading<TSensor>>
{
    TSensor Sensor { get; }
}

It leads to the following errors:

The type ISensor<TReading> must be convertible to
ISensor<ISensorReading<ISensor<TReading>>> in order to use it as parameter TSensor in the generic type or method ISensorReading<TSensor>

and

The type ISensorReading<TSensor> must be convertible to
ISensorReading<ISensor<ISensorReading<TSensor>>> in order to use it as parameter TReading in the generic type or method ISensor<TReading>

I fear it is due to an insolvable circular reference at compile time; however I want to ensure congruence for derived types like TelemetricSensor : ISensor<TelemetricReading> and
TelemetricReading : ISensorReading<TelemetricSensor>

Which other aproaches should I use to allow simple casting and type safe?

I am using .NET 2.0 and VS2005

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:50:47+00:00Added an answer on May 28, 2026 at 7:50 am

    Thanks all for your answers.

    Finally this is the solution I choose for the problem. Hope to be usefull. If you have comments or know a way to impriove it without change the framework, let me know.

    First I created an ISensor interface with the minimal requirements. After that I defined a new ISensor generic interface as follows:

    public delegate void SensorErrorEventHandler<TSensor>(TSensor sensor, ISensorError error)
        where TSensor : ISensor;
    
    public delegate void SensorReadingCompletedEventHandler<TSensor, TReading>(TSensor sensor, TReading[] read)
        where TSensor : ISensor
        where TReading : ISensorReading<TSensor>;
    
    public interface ISensor : IDisposable
    {
        bool IsOpen { get; }
        bool Started { get; }
        void Connect();
        void Disconnect();
        void Start();
        void Stop();
    }
    
    public interface ISensor<TSensor, TReading> : ISensor
        where TSensor : ISensor
        where TReading : ISensorReading<TSensor>
    {
        TReading[] LastReadings { get; }
        event SensorErrorEventHandler<TSensor> Error;
        event SensorReadingCompletedEventHandler<TSensor, TReading> ReadCompleted;
        bool Read(out TReading[] readings);
    }
    
    public interface ISensorReading<TSensor> where TSensor : ISensor
    {
        TSensor Sensor { get; }
        bool Mistaken { get; }
    }
    

    With theese interfaces defined and following the same structures, I was able to made the first implementation: A TelemetricSensor class with its correspondient ITelemetricReading

    public delegate void TelemetricSensorThresholdExceededEventHandler<TSensor>(TSensor sensor)
        where TSensor : ITelemetricSensor;
    
    public interface ITelemetricSensor : ISensor
    {
        /* Properties, events and methods */
    }
    
    public interface ITelemetricReading : ISensorReading<ITelemetricSensor>
    {
        /* Properties, events and methods */
    }
    
    public abstract class TelemetricSensor<TSensor, TReading> : ITelemetricSensor, ISensor<TSensor, TReading>
        where TSensor : ITelemetricSensor
        where TReading : ITelemetricReading, ISensorReading<TSensor>
    {
        public abstract TReading[] LastReadings { get; }
        public event SensorErrorEventHandler<TSensor> Error;
        public event SensorReadingCompletedEventHandler<TSensor, TReading> ReadCompleted;
    
        public abstract bool Read(out TReading[] readings);
    }
    

    Here TReading of the TelemetricSensor abstract class is interesting. It is defined as

    TReading : ITelemetricReading, ISensorReading<TSensor>
    

    which may appear to be redundant because ITelemetricReading inherits from ISensorReading, but it is required to get the code compile and met the requirement for both

    TReading[] LastReadings { get; }
    

    property and

    bool Read(out TReading[] readings);
    

    method. Finally to skip tedious casting (and perhaps some errors) mutiple overloads of Read(…) can be done so each provides the data casted correctly and also met all interface implementations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, please accept my apologies if this question is basic, I mainly have
My apologies, I've actually asked this question multiple times, but never quite understood the
This is my first step into the world of stackoverflow, so apologies if I
I have been in this situation quite a few times where visual studio does
Apologies if I am not wording this correctly but what I have at the
First of all my apologies to those of you who would have followed my
First off, apologies if this is a complete noob question which could easily be
I assume this question is language agnostic, and apologies if it's quite rudimentary, but
First off - apologies if this or a similar question has been asked before.
My apologies if this has been asked before, I wasnt quite sure if this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.