Question:You need to write code equivalent to the following, but using reflection:
Dim d As New DateTime(2008, 5, 1)
Console.WriteLine(d.ToShortDateString())
Which code sample does this correctly?
 

A Dim t As Type = GetType(DateTime)
Dim mi As MethodInfo = t.GetConstructor( _
New Type() {GetType(Integer), GetType(Integer), GetType(Integer)})
Dim d As Object = mi.Invoke(New Object() {2008, 5, 1})
Dim dToShortDateString As ConstructorInfo = t.GetMethod("ToShortDateString")
Console.WriteLine(DirectCast(dToShortDateString.Invoke(d, Nothing), String)) 

B Dim t As Type = GetType(DateTime)
Dim ci As ConstructorInfo = t.GetConstructor(New Object() {2008, 5, 1})
Dim d As Object = ci.Invoke( _
New Type() {GetType(Integer), GetType(Integer), GetType(Integer)})
Dim dToShortDateString As MethodInfo = t.GetMethod("ToShortDateString")
Console.WriteLine(DirectCast(dToShortDateString.Invoke(d, Nothing), String))
 

C Dim t As Type = GetType(DateTime)
Dim dToShortDateString As MethodInfo = t.GetMethod("ToShortDateString")
Console.WriteLine(DirectCast(dToShortDateString.Invoke(t, Nothing), String))
 

D Dim t As Type = GetType(DateTime)
Dim ci As ConstructorInfo = _
t.GetConstructor(New Type() {GetType(Integer), GetType(Integer),
GetType(Integer)})
Dim d As Object = ci.Invoke(New Object() {2008, 5, 1})
Dim dToShortDateString As MethodInfo = t.GetMethod("ToShortDateString")
Console.WriteLine(DirectCast(dToShortDateString.Invoke(d, Nothing), String))
 

+ Answer
+ Report
Total Preview: 1091

Copyright © 2024. Powered by Intellect Software Ltd