0

Say I have two interfaces, IFeaturesA, IFeaturesB.

IFeaturesA has a set of signature methods. Let's say one is:-

public void printMe();

IFeaturesB implements IFeaturesA, and adds a new signature method, such as:-

public void printMeAlso();

Say I want to use a generic method, such as:-

public Check<E>(E passedItem)
{

}

If i passed in IFeaturesA, I want to be able to call the methods of this. If i pass in IFeaturesB, I want to be able to call the extra method, printMeAlso();

What is the best way to check for an interface type passed into a generic method and access its methods in C#? Is checking for the object type against the two interface types and then casting to the type the optimal way?

7 Answers 7

4

That does not fit the the generics.

Generics is a typeless reuse of the behaviour. If you need to check the type then it is not a good fit for generics. You may impose constraints but checking the actual type must not be done in the generic method..

All you are trying to abstract is the fact that you are passing an item. That is not an abstraction.

Sign up to request clarification or add additional context in comments.

1 Comment

Alright, makes sense. I wanted to cut down on having to write another method to call similar (slightly extended) functions from one of the interfaces from the other. But I guess having to write if comparison statements can be just as bloaty.
2

use two methods

public Check(IFeaturesA passedItem) {

}

public Check(IFeaturesB passedItem) {

}

add private methods to handle commonality

1 Comment

Thanks for all your answers, I went with this as this was along the lines of what i was going to do initially, I just thought perhaps there was a less bloaty way of doing it, but it turns out this is just as efficient.
1

Don't think it's a good way to achieve what you want by defining an interface, simply define classe structure, like

public class FeaturesA
{
    public virtual void printMe()
    {  
    }

}

public class FeaturesB : FeaturesA
{
    public override void printMe()
    {  
    }

}

and after use in code, like

FeaturesA a = new FeaturesA(); 
FeaturesA b = FeaturesB();
public Check(A passedItem)
{
    passedItem.printMe();
}

on Check(a) prints a, on Check(b) prints b

Hope this helps.

3 Comments

I'm using the interfaces to define method signatures for a plethora of other classes - ie one has a printA functionality, the next has printA AND printB functionality, another may have printA and printB, but each class implements the method differently.
@Davos555: but that doesn't change anythign from this perspective. You still have signature, you still can have (if you need) other methods, and you can use overloading, which is in case of Interfaces with Generics is not possible to achieve in a way you want it.
Thanks Tigran, was just trying to explain a bit better.
1

If you want to check the type of an item, use "is":

if (passedItem is IFeatureA) { }

If you want to constrain a generic to certain behaviors that can be defined by an interface or an abstract class, use "where":

public class foo<E> where E : IFeatureA
{
}

The latter case won't let you use IFeatureB, but any variable in the class of type E can use methods and properties defined in IFeatureA without checking type.

You might also want to look at type Dynamic, since it's closer to what you want to do.

Comments

0

I would check the type of E like

if(E.GetType().Equals(typeof(IFeaturesA)){ //call method...}

Comments

0

This looks like a call for another interface.

If you let both interfaces inherit from IPrintable which only contains a Print() method, and extend your Check<E>(E passedItem) with where E : IPrintable, you can call passedItem.Print().

Comments

0

try the following

Check(IFeaturesA f) {}
Check(IFeaturesB f){}
Check<T>(T obj) where T : IFeaturesA, IFeaturesB {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.