直观测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public partial class NaviControl : UserControl, ITestControl { public NaviControl() { InitializeComponent(); Load += NaviControl_Load; } private void NaviControl_Load( object sender, EventArgs e) { Debug.Print($ "Base.IsAssignableFrom:" ); Debug.Print($ "{typeof(UserControl).IsAssignableFrom(this.GetType())}" ); //True Debug.Print($ "{typeof(ITestControl).IsAssignableFrom(this.GetType())}" ); //True Debug.Print($ "This.IsAssignableFrom:" ); Debug.Print($ "{this.GetType().IsAssignableFrom(typeof(UserControl))}" ); //False Debug.Print($ "{this.GetType().IsAssignableFrom(typeof(ITestControl))}" ); //False Debug.Print($ "Base.IsInstanceOfType:" ); Debug.Print($ "{typeof(UserControl).IsInstanceOfType(this)}" ); //True Debug.Print($ "{typeof(ITestControl).IsInstanceOfType(this)}" ); //True Debug.Print($ "This.IsSubclassOf:" ); Debug.Print($ "{this.GetType().IsSubclassOf(typeof(UserControl))}" ); //True Debug.Print($ "{this.GetType().IsSubclassOf(typeof(ITestControl))}" ); //False Debug.Print($ "This.Is:" ); Debug.Print($ "{(this is UserControl)}" ); //True Debug.Print($ "{(this is ITestControl)}" ); //True } } |