473,800 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ContainsInterfa ce<A, Q>(A[] list)
{
foreach (A a in list)
if (a is Q)
return true;
return false;
}

and is used like

ContainsInterfa ce<Type, IEnumerable>(fi eld.FieldType.G etInterfaces()) )

So it checks the list for elements that implement IEnumerable.

I used this successfully in a "ContainsTy pe"

ContainsType<At tribute, RawExcludeAttri bute>(attribute s)

Where its the same code. The above essentially just checks if any of the
attribute's in attributes is a RawExcludeAttri bute. 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 = "IEnumerabl e" FullName = "System.Collect ions.IEnumerabl e"}
System.Type {System.Runtime Type}
a = + a {Name = "IEnumerabl e" FullName = "System.Collect ions.IEnumerabl e"}
System.Type {System.Runtime Type}
So I don't see why they are not the same types ;/

Thanks,

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

t = + t {Name = "IEnumerabl e" FullName =
"System.Collect ions.IEnumerabl e"} System.Type {System.Runtime Type}
a = + a {Name = "IEnumerabl e" FullName =
"System.Collect ions.IEnumerabl e"} System.Type {System.Runtime Type}
So I don't see why they are not the same types ;/

I do see something like a.GetType().ToS tring() 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 ContainsInterfa ce(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
1409
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 VB-types <G> BTW, I am somewhat proficient in C#, but my bosses and coworkers barely know VB, so no, I can't just rewrite it in C# <g> Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your...
5
17040
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, not an object I am wanting to check. Here's my scenario. I am dynamically loading assemblies (dlls) at runtime. I am getting the list of types from each assembly, and if that type implements a particular interface, then and only then do I want...
8
5736
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 simplest form, a BHO is a COM in-
4
12393
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 throw an error... Is there a better way I can do this via System.Reflection?
1
1013
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 this way: <Assembly: AssemblyKeyFile("..\..\keyfile.snk")> The code:
4
3155
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
1590
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
2113
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 the objects methods to authenticate. In the meantime a property called "ProjectConnected", passes from 0 to 1. So I think the object is correctly instanciated and the connection works.
11
15659
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: Class RobertNestaMarley implements Smoker_Interface the operator instanceof will let me know. like this:
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10275
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.