Hello,
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.