Skip to content

Commit 981b2dd

Browse files
committed
Added reflection examples
1 parent 9bba519 commit 981b2dd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

development/ps/reflection.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# constructor
2+
$constructorInfo = [System.IO.FileInfo].GetConstructor([string].AsType())
3+
$constructorInfo = [System.IO.FileInfo].GetConstructor("public,instance", $types)
4+
$constructorInfo.Invoke('C:\Windows')
5+
6+
# private field
7+
$obj = [System.IO.FileInfo]::new('C:\Windows')
8+
$field = [System.IO.FileInfo].GetField("_name", "nonpublic,instance")
9+
# longer version
10+
$field = [System.IO.FileInfo].GetField("_name", [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance)
11+
$field.GetValue($obj)
12+
13+
# private method - static
14+
$obj = $null
15+
$privMethod = [System.DateTime].GetMember("DateToTicks", "nonpublic,static")
16+
$privMethod.Invoke($obj, @(2023, 10, 10))
17+
18+
# private method - instance
19+
$obj = [System.DateTime]::Now
20+
$privMethod = [System.DateTime].GetMember("GetDatePart", "nonpublic,instance")
21+
$privMethod.Invoke($obj, @(0))
22+
23+
# private method - args requirements
24+
$privMethod = [System.DateTime].GetMethod("DateToTicks", "nonpublic,static", $null, @([int], [int], [int]), @())
25+
$privMethod.Invoke($obj, @(2023, 10, 10))

0 commit comments

Comments
 (0)