473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to determine whether a type implements a generic interface with aself-referential constraint

I have an interface that looks something like this:
public interface IFoo<Twhere T : IFoo<T{ ... }

Inside a class that looks something like this...
public class Bar<V{ ... }
....I would like to find out if V implements IFoo<V>. However, I can't
do this...
if (typeof(IFoo<V>).IsAssignableFrom(typeof(V))) { ... }
....because it's circular: the compiler won't let us refer to IFoo<V>
because we don't know if V implements IFoo<V>. The best I can do is
this:

bool implementsInterface = true;
try
{
typeof(IFoo<>).MakeGenericType(typeof(V));
}
catch (ArgumentException)
{
// Oops! I guess not.
implementsInterface = false;
}

However, I don't get the impression this is the "right" way to do it.
I don't like the idea of catching an ArgumentException in general. Is
there some other way to do this?
Jun 27 '08 #1
4 1853
On Jun 5, 5:13 pm, Weeble <clockworksa...@gmail.comwrote:

<snip>
However, I don't get the impression this is the "right" way to do it.
I don't like the idea of catching an ArgumentException in general. Is
there some other way to do this?
You could get all the interfaces that the type implements (potentially
recursively). For each of them, see if the generic type ==
typeof(IFoo<>), and then check the generic type argument to see if
it's typeof(V).

Jon
Jun 27 '08 #2
So working from Jon's idea, perhaps something like the code below would
work?

---------------------------
using System;
using System.Collections.Generic;
using System.Text;

public interface IFoo<Twhere T : IFoo<T>
{
}

class SomeClass : IFoo<SomeClass>
{
}

class Bar<V>
{
public static bool ImplementsInterface()
{
string interfaceName = typeof(IFoo<>).Name;
Type theInterface = typeof(V).GetInterface(interfaceName);
if (theInterface != null)
{
Type deff = theInterface.GetGenericTypeDefinition();
return (deff == typeof(IFoo<>));
}
else
return false;
}
}

class Program
{
static void Main(string[] args)
{
bool result1 = Bar<SomeClass>.ImplementsInterface();
Console.WriteLine(result1.ToString());

bool result2 = Bar<string>.ImplementsInterface();
Console.WriteLine(result2.ToString());

Console.Read();
}
}
---------------------------

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:2e**********************************@i76g2000 hsf.googlegroups.com...
On Jun 5, 5:13 pm, Weeble <clockworksa...@gmail.comwrote:

<snip>
>However, I don't get the impression this is the "right" way to do it.
I don't like the idea of catching an ArgumentException in general. Is
there some other way to do this?

You could get all the interfaces that the type implements (potentially
recursively). For each of them, see if the generic type ==
typeof(IFoo<>), and then check the generic type argument to see if
it's typeof(V).

Jon
Jun 27 '08 #3
On Jun 6, 12:53 am, "Rene" <a...@b.comwrote:
So working from Jon's idea, perhaps something like the code below would
work?
[snip]

That's great, thank you both! I think to be safe I'd still need to
check the generic type argument, otherwise it could break like this:

class CheatClass : SomeClass { }

Which gives {Bar<CheatClass>.ImplementsInterface()==True}, which isn't
quite what I want to check. But it's clear how to handle that, so
thanks!

Of course, now there's the question of whether there's a better way
than wanton use of reflection to get out of the generic tangle that
resulted in this. Sadly it's not easy to show an isolated example, so
I think I'll leave it for another time. In general terms, would people
say that *sometimes* it's better just to suffer a bit of typecasting
rather than try to preserve types with complex application of
generics, or is there *always* a good generic solution that guarantees
type-safety at compile-time?
Jun 27 '08 #4
On Jun 6, 3:14 pm, Weeble <clockworksa...@gmail.comwrote:

<snip>
Of course, now there's the question of whether there's a better way
than wanton use of reflection to get out of the generic tangle that
resulted in this. Sadly it's not easy to show an isolated example, so
I think I'll leave it for another time. In general terms, would people
say that *sometimes* it's better just to suffer a bit of typecasting
rather than try to preserve types with complex application of
generics, or is there *always* a good generic solution that guarantees
type-safety at compile-time?
The former. Things will improve slightly if/when C# 4 includes generic
variance, but it'll never go away entirely. It's always worth *trying*
to see if there's a generic solution to solve the problem, but it
won't always happen.

Jon
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
21
by: Walter L. Preuninger II | last post by:
I would like to write a generic procedure that will take string or numeric variables. I can not think of a way to make this more clear except to show what I want. int main(void) { int i=7;...
3
by: Sathyaish | last post by:
I wanted to practice some Linked List stuff, so I set out to create a linked list. The plan was to create the following: (1) A linked list class in Visual Basic (2) A non-class based linked list...
8
by: Leszek Taratuta | last post by:
Hello, I have the following code: // Load the MenuBar.ascx user control. The control defines SavePropertyEvent event. UserControl ctrl =...
2
by: Tosch | last post by:
I have written an interface, let's call it AddinInterface1. At some stage I had to add more properties to the interface. So I wrote a new interface, AddinInterface2 which inherits AddinInterface1....
3
by: Dave Booker | last post by:
Am I missing something here? It looks like the generic Queue<T> implements Enumerable<T> and ICollection but not ICollection<T>. (I want to use it in an interface that wants an ICollection<T>.) ...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
4
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am a newbie to WCF so please forgive if this is an obvious question. I have the following. (Service contract) IEmployee public interface IEmployee { List<EmployeeGetEmployeeByID(string...
5
by: PJ6 | last post by:
I'd like to be able to declare a function where it always returns the type of the inheritor. To show this in as simple an example that I can think of, where MyClassType is a reflexive generic type...
7
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.