Inheriting multiple interfaces without using interfaces

Yes, the title doesn't make a lot of sense, but here's my situation.

I have two interfaces, say IGen and ITrans. Some of my classes implement IGen, some implement ITrans, and some implement both

ITrans has one method (Translate) and IGen has one method (Generate). For classes that implement both ITrans and IGen Generate, simply call Translate.

I also have an IGenAndTrans interface which is simply defined as

public interface IGenAndTrans : IGen , ITrans
{        
}

      

And I have a class (call it Holder) that has IGenAndTrans as a property.

[Serializable] //<- Problem
public class Holder
{
    public IGenAndTrans GeneratorAndTranslator { get; set;}
}

      

Now, I want to mark the Holder of the class with [SerializableAttribute] and use the XmlSerializer. However, I cannot do this because Holder has a property that is an interface. The generally recommended approach would be to make IGenAndTrans an abstract base class and use XmlInclude. I have done this successfully in the past.

However, I am not sure how to do it in this case. Since many of my classes implement both IGen and ITrans, they cannot simply inherit from an abstract base class. This would mean that I would need to split each of these classes into two classes with corresponding code duplication (since Generate calls translates a lot of time)

Any recommendations (if I was able to explain myself well enough)? Perhaps I am too close to the code and should implement it differently.

+2


source to share


3 answers


I guess this is a hoax because I have inside information, but I managed to customize the fix a bit for myself.

I realized that everything IGen implemented also ITrans implemented. And I realized that in every class that implemented both interfaces, Generate is always called Translate.



So, I created a basic abstract class GenerateAndTranslate that has one property called Translator and one of them is called Generator. Both of them have a new type called TranslatorBase, which inherits everything previously implemented by ITrans.

This fix only works in my case, so I'm still curious about other people's opinions.

0


source


since this is an autoproperty, perhaps you could use [field: NonSerialized]

to indicate that the backing field is not serialized?



0


source


I was about to answer your question, but realized that there really isn't enough information / context on this issue to come up with a solid idea.

But the general idea is to store data, not methods.

For example, if you have a Tiger class and a Wolf class, you should call them Animal instead of IWalk; on the other hand, if you have a Tiger class and a Robot class, you may have to refer to them as an object, even if they use the IWalk interface.

You should only refer to objects as interfaces, when you don't need data, but methods.

On an unrelated note, there seems to be a tendency in the C # world to be strong type (which is understandable since it provides type safety and compile time checking), but there is always the option to use generic types and cast them on the fly.

0


source







All Articles