There's probably a lot of ways to do what you want.  Here is one.

I would use the "as" operator to try and cast the object to an interface
reference.  If it fails the interface reference will be null, otherwise you
can begin to use it.

Another idea would be to use the "is" operator.

If you check the docs you will see that

IRemotingAnnouncement ira = o as IRemotingAnnouncement;

Is the same as

IRemotingAnnouncement ira = ( o is IRemotingAnnouncement ) ?
(IRemotingAnnouncement)o : (IRemotingAnnouncement)null;

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Jekke Bladt
Sent: Tuesday, November 15, 2005 12:46 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] 1.1: Type comparison with Interfaces

All--

I have a custom collection class called RemotingAnnouncementCollection,
which should hold objects of any type that implements the
IRemotingAnnouncement interface. In the collection class's validator, I
have the following code:

protected override void OnValidate( Object value )
{
        if ( value.GetType() !=
Type.GetType("GroupOne.Utilities.Remoting.IRemotingAnnouncement") )
                throw new ArgumentException( "value must be of type
IRemotingAnnouncement--not " +
                        value.GetType().ToString());
}

This seems to throw an exception in all cases. How do I replace "if (
value.GetType() !=
Type.GetType("GroupOne.Utilities.Remoting.IRemotingAnnouncement") )"
with something that says "if value.GetType() is of any type that
implements GroupOne.Utilities.Remoting.IRemotingAnnouncement?"

TIA

--Jekke

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to