none
c# Multiple Inheritance RRS feed

  • Question

  • I want to create a class that inherits from 2 existing classes :

    1. BsonDocument

    2. Messages (class which exist in dll and i can't change it)

    is it possible to make a new class that will inherit from both of them?

    Tuesday, May 21, 2019 7:52 AM

Answers

  • Not it doesn't, basically. Class inheritance can be only from one parent class. You can inhered from more interfaces only. 

    You can create interface of BsonDocument and implement it in your class which can be inherited from Messages. But I think there will be some smell in design.

    Tuesday, May 21, 2019 8:08 AM
  • In these types of situations when you cannot modify the base classes I would tend to recommend going the aggregate route.

    public class BsonDocumentMessage
    {
       //Making these properties but you'd need to decide how to expose these underlying objects (if at all)
       public BsonDocument Document { get; set; }
       public Messages Message { get; set; }
    }

    The nice thing about aggregation is that you can wrap as little or as much of the underlying objects as you need. Unfortunately callers now have to do this.

    void SomeMessageFunction ( Messages message )
    { }
    
    void SomeDocumentFunction ( BsonDocument document )
    { }
    
    var data = new BsonDocumentMessage();
    ...
    
    SomeMessageFunction(data.Message);
    SomeDocumentFunction(data.Document);
    I personally don't have a problem with this but if you really wanted to hide the details then you could define type converters to convert the aggregate to/from the underlying types. However you'd want to be very careful that you don't inadvertently mess up the object by allowing (potentially implicit) conversions to one of the types.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 23, 2019 2:55 PM
    Moderator

All replies

  • Not it doesn't, basically. Class inheritance can be only from one parent class. You can inhered from more interfaces only. 

    You can create interface of BsonDocument and implement it in your class which can be inherited from Messages. But I think there will be some smell in design.

    Tuesday, May 21, 2019 8:08 AM
  • C# and .NET support single inheritance only. So, as Petr already told you, you can only inherit from a single class. But, you said you aren't able to change the Messages class, but seemingly are able to change BsonDocument. That would allow you to define an inheritance hierarchy from Messages => BsonDocument => YourNewClass.

    That way you might be able to overcome the single inheritance limitations of C#.

    wizend
    Tuesday, May 21, 2019 10:24 AM
  • Maybe consider something like this:

    class MyClass : BsonDocument
    {
        public readonly Messages Messages;
    
        public MyClass( )
        {
            Messages = new Messages( );
        }
    
        public static implicit operator Messages( MyClass c )
        {
            return c.Messages;
        }
    }
    

    Tuesday, May 21, 2019 10:39 AM
  • Hi 

    Thank you for posting here.

    Based on your description, you want to make a new class that will inherit from two classes.

    As Petr' said, it is hard to do that. The following link is similar to your problem, you could have a look.

    https://stackoverflow.com/questions/11134832/inherit-from-two-classes-in-c-sharp​​​​​​​

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 1:35 AM
  • In these types of situations when you cannot modify the base classes I would tend to recommend going the aggregate route.

    public class BsonDocumentMessage
    {
       //Making these properties but you'd need to decide how to expose these underlying objects (if at all)
       public BsonDocument Document { get; set; }
       public Messages Message { get; set; }
    }

    The nice thing about aggregation is that you can wrap as little or as much of the underlying objects as you need. Unfortunately callers now have to do this.

    void SomeMessageFunction ( Messages message )
    { }
    
    void SomeDocumentFunction ( BsonDocument document )
    { }
    
    var data = new BsonDocumentMessage();
    ...
    
    SomeMessageFunction(data.Message);
    SomeDocumentFunction(data.Document);
    I personally don't have a problem with this but if you really wanted to hide the details then you could define type converters to convert the aggregate to/from the underlying types. However you'd want to be very careful that you don't inadvertently mess up the object by allowing (potentially implicit) conversions to one of the types.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 23, 2019 2:55 PM
    Moderator