Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I restrict generic method to more than one interface?

Tags:

c#

generics

where

I have a generic method

public static void DoSomething<T>()
{...}

. Now I want to restrict that T.

public static void DoSomething<T>() where T: IInterface1
{...}

But what I really want is allowing multiple interfaces, something like

public static void DoSomething<T>() where T: IInterface1, IInterface2
{...}

But that doesn't work. Compiler says something like

There's no implicit conversion from IInterface1 to IInterface2

There's no implicit conversion from IInterface2 to IInterface1

I thought about letting the classes implement a common interface which I can refer to but I don't have access to the classes.

What possibilities do I have to allow multiple Interfaces?

Thanks, Tobi

Edit: Here's what I wanted to do. I'm developing an Outlook-Add-In. I use this piece of code below quite often.

    public static object GetItemMAPIProperty<T>(AddinExpress.MAPI.ADXMAPIStoreAccessor adxmapiStoreAccessor, object outlookItem, uint property) where T: Outlook.MailItem, Outlook.JournalItem
    {
        AddinExpress.MAPI.MapiItem mapiItem;
        mapiItem = adxmapiStoreAccessor.GetMapiItem(((T)outlookItem));
        return mapiItem != null ? mapiItem.GetProperty(property) : null;
    }

The method GetMapiItem takes an object as long as it's one of Outlook's items (Journal, Mail, Contact,...). That's why I was restricting T. Because it cannot be, say, Outlook.MAPIFolder.

No I've changed the method to

    public static object GetItemMAPIProperty<T>(AddinExpress.MAPI.ADXMAPIStoreAccessor adxmapiStoreAccessor, T outlookItem, uint property)
    {
        AddinExpress.MAPI.MapiItem mapiItem;
        mapiItem = adxmapiStoreAccessor.GetMapiItem(((T)outlookItem));
        return mapiItem.GetProperty(property);
    }

but the developer (In this case I) can give it any Type because the method GetMapiItem accepts an object. I hope that makes sense. I'm not sure if it does for that example but I guess restricting a generic method to multiple Types (with OR) can be a good idea.

like image 975
Tobias Avatar asked Jul 28 '09 16:07

Tobias


People also ask

How do you restrict a generic type?

You can specify one or more constraints on the generic type using the where clause after the generic type name. The following example demonstrates a generic class with a constraint to reference types when instantiating the generic class.

Can a generic class have multiple constraints?

There can be more than one constraint associated with a type parameter. When this is the case, use a comma-separated list of constraints. In this list, the first constraint must be class or struct or the base class.

Can you inherit from two interfaces with the same method name in both of them?

C# allows the implementation of multiple interfaces with the same method name.

Can a generic be an interface?

Java Generic Classes and SubtypingWe can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses.


1 Answers

This compiles fine for me:

interface I1 { int NumberOne { get; set; } }
interface I2 { int NumberTwo { get; set; } }

static void DoSomething<T>(T item) where T:I1,I2
{
    Console.WriteLine(item.NumberOne);
    Console.WriteLine(item.NumberTwo);
}

So the syntax seems fine... perhaps its something else that's causing the problem.

like image 193
Nader Shirazie Avatar answered Nov 01 '22 14:11

Nader Shirazie