473,406 Members | 2,378 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,406 software developers and data experts.

check if object implements interface not working ;/

I wrote this function that is suppose to returnt rue if an array of
types(actually stuff returned by GetInterfaces) implements the interface Q.
private bool ContainsInterface<A, Q>(A[] list)
{
foreach (A a in list)
if (a is Q)
return true;
return false;
}

and is used like

ContainsInterface<Type, IEnumerable>(field.FieldType.GetInterfaces()))

So it checks the list for elements that implement IEnumerable.

I used this successfully in a "ContainsType"

ContainsType<Attribute, RawExcludeAttribute>(attributes)

Where its the same code. The above essentially just checks if any of the
attribute's in attributes is a RawExcludeAttribute. It works fine. My logic
was the same for interfaces but it doesn't work. (Maybe interfaces cannot
implement themselfs as "a is Q" is always false... yet when I debug I get
exactly these type for a and typeof(Q).

Even something like

foreach (A a in list)

{

Type t = typeof(Q);

if (a.GetType() == typeof(Q))

return true;

}

does not work.

In this case

t = + t {Name = "IEnumerable" FullName = "System.Collections.IEnumerable"}
System.Type {System.RuntimeType}
a = + a {Name = "IEnumerable" FullName = "System.Collections.IEnumerable"}
System.Type {System.RuntimeType}
So I don't see why they are not the same types ;/

Thanks,

Jon
Apr 2 '08 #1
3 6541
In this case

t = + t {Name = "IEnumerable" FullName =
"System.Collections.IEnumerable"} System.Type {System.RuntimeType}
a = + a {Name = "IEnumerable" FullName =
"System.Collections.IEnumerable"} System.Type {System.RuntimeType}
So I don't see why they are not the same types ;/

I do see something like a.GetType().ToString() always returns the RunTime
Type and that is probably the reason why its not working but I don't see how
to fix it.
Apr 2 '08 #2
NM, was being stupid on what I was doing. Was checking to see if the
interfaces contained an interface but all I had to do was check and see if
the object itself implemented it(using is).
Apr 2 '08 #3

"Jon Slaughter" wrote:
NM, was being stupid on what I was doing. Was checking to see if the
interfaces contained an interface but all I had to do was check and see if
the object itself implemented it(using is).
Indeed, if you have an object this should work fine

if(myObject is IEnumerable) { ... }

If, however, you only have the types then something like this should work

public bool ContainsInterface(Type A, Type Q)
{
Type[] interfaces = A.GetInterfaces();
foreach (Type type in interfaces)
{
if (type == Q)
return true;
}

return false;
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Apr 2 '08 #4

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

Similar topics

4
by: Jeff Molby | last post by:
First off, this is a VB application, but I know you C# are used to working at the lower levels like this, so I'm hoping this is an easy one for you. Then you can continue to feel superior to us...
5
by: Joe Rattz | last post by:
Ok, I am trying to figure out how to tell if a class (not object) implements a particular interface. Using the "is" operator does not seem to work for me and I assume its because I have a class,...
8
by: Joe Johnston | last post by:
I need a Browser Helper object written in VB.NET Please point me at a good example. Joe MCPx3 ~ Hoping this MSDN ng three day turnaround is true. Additional info: What is a BHO? In its...
4
by: Lucas Tam | last post by:
Hi all, I like to dynamically check if an object implements a particular interface. Currently I am doing: Ctype(Object, IInterface) If the object implements the interface it will not...
1
by: Serge M. Ignatkin | last post by:
Hello, after a lot of readings I've got something working. Can you please check -- is it enough? .Net server and COM-client, also I generated key file and place "ref" to it in AssemblyInfo.vb...
4
by: pipehappy | last post by:
Hello everyone: Is there a way to check the type when do assignment? if I write: ab = bc and want to make sure the return value of isinstance(bc, klass) is True or I will raise a exception.
5
by: Random | last post by:
How can I use reflection (or some other method) to find the type of an object that has been passed in to my method under an interface definition? I try to use GetType, but that won't work.
16
by: Alex | last post by:
Hello, I'm trying to use a (remote) COM object from a PHP script (4.4, server has apache2 win32). The basics seem to work : I instantiate the COM object ($o = new COM"..."), then I use two of...
11
bilibytes
by: bilibytes | last post by:
Hello, I have a little question. I would like to know how to check if an object is compliant with an interface. I know that if the Class implements explicitly the Interface, like this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.