-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReflection.cs
68 lines (59 loc) · 1.67 KB
/
Reflection.cs
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Reflection demos
//
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
namespace ReflectionDemos
{
public class Base
{
public string GetName()
{
return this.GetType().Name;
}
public static string GetStaticName()
{
return MethodBase.GetCurrentMethod().DeclaringType.Name;
}
}
public class Derived : Base
{
public string GetDerivedName()
{
return this.GetType().Name;
}
public string GetBaseClassName()
{
return this.GetType().BaseType.Name;
}
// The new keyword overrides the base classes' static method. Otherwise static methods
// can only be defined once per inheritance chain.
public new static string GetStaticName()
{
return MethodBase.GetCurrentMethod().DeclaringType.Name;
}
}
[TestClass]
public class ReflectionDemos
{
[TestMethod]
public void WhenBase_ExpectBaseClassName()
{
Assert.AreEqual("Base", new Base().GetName());
Assert.AreEqual("Base", Base.GetStaticName());
}
[TestMethod]
public void WhenDerived_ExpectDerivedClassName()
{
Assert.AreEqual("Derived", new Derived().GetName());
Assert.AreEqual("Derived", new Derived().GetDerivedName());
Assert.AreEqual("Derived", Derived.GetStaticName());
}
[TestMethod]
public void WhenDerived_ExpectCanSeeBaseClassName()
{
Assert.AreEqual("Base", new Derived().GetBaseClassName());
}
}
}