Is there a function in the assembly namespace that allows you to test whether a type retrieved by "typeof" or .GetType(); inherits from a given interface?
So for example:
using System;
namespace Test
{
public interface iTest
{
string MyString();
}
public class cTest : iTest
{
public string MyString()
{
return "Test String.";
}
}
public class Program
{
static void Main()
{
{
cTest c = new cTest();
Console.WriteLine(c.GetType().ToString());
if (typeof(iTest) == c.GetType())
{
Console.WriteLine("Match.");
}
}
}
}
}
The object "c" is of type "cTest". However, I want to test if "cTest" implements interface "iTest".
fm

1 answers
Haha, I found it, I did some searching in a C# language specification and found the "is" operator does what I need:
if (c is iTest)
{
Console.WriteLine("Interface implemented.");
}
Need to really find some more advanced code samples for C#...I'm sure there's a lot of the language I'm still not familiar with even though I already am very good at it.
Thanks
fm
answered 2 years ago by:
490
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!