C#中判断一个类是否实现了某interface
1
using System;
2
using System.Threading;
3
using System.Text;
4
using System.Net;
5
using System.Net.Sockets;
6
using System.IO;
7
using System.Data;
8
using System.Collections;
9
using System.Collections.Specialized;
10
using System.Data.Odbc;
11
using System.Text.RegularExpressions;
12
13
14
public interface IOne
15
{
16
string GetOne();
17
}
18
19
public interface ITwo
20
{
21
string GetTwo();
22
}
23
24
public class One : IOne
25
{
26
public string GetOne()
27
{
28
return "one-getone";
29
}
30
}
31
32
public class OneTwo : IOne, ITwo
33
{
34
public string GetOne()
35
{
36
return "OneTwo-getone";
37
}
38
39
public string GetTwo()
40
{
41
return "OneTwo-GetTwo";
42
}
43
}
44
45
public class Ware
46
{
47
static void Main(String[] args) {
48
One oo = new One();
49
OneTwo ot = new OneTwo();
50
Console.WriteLine(oo is IOne);
51
Console.WriteLine(oo is ITwo);
52
53
Console.WriteLine(ot is IOne);
54
Console.WriteLine(ot is ITwo);
55
}
56
57
58
}
using System;2
using System.Threading;3
using System.Text;4
using System.Net;5
using System.Net.Sockets;6
using System.IO;7
using System.Data;8
using System.Collections;9
using System.Collections.Specialized;10
using System.Data.Odbc;11
using System.Text.RegularExpressions;12

13

14
public interface IOne15
{16
string GetOne();17
}18

19
public interface ITwo20
{21
string GetTwo();22
}23

24
public class One : IOne25
{26
public string GetOne()27
{28
return "one-getone";29
}30
}31

32
public class OneTwo : IOne, ITwo33
{34
public string GetOne()35
{36
return "OneTwo-getone";37
}38

39
public string GetTwo()40
{41
return "OneTwo-GetTwo";42
}43
}44

45
public class Ware46
{47
static void Main(String[] args) {48
One oo = new One();49
OneTwo ot = new OneTwo();50
Console.WriteLine(oo is IOne);51
Console.WriteLine(oo is ITwo);52
53
Console.WriteLine(ot is IOne);54
Console.WriteLine(ot is ITwo);55
}56

57
58
}




浙公网安备 33010602011771号