Hledat English (United States)  Čeština (Česká Republika) Slovenčina (Slovenská Republika)  Hrvatski (Hrvatska)
24. července 2017 ..:: Software » Blog ::.. Zaregistrovat se  Přihlásit se
   Novinky Minimalizovat  
     
 Vytiskout   
   Blog Minimalizovat  
II 11

Přidal: Đonny
11.2.2010 21:52

57.         Get… operators

Does not need changes in CLI

What exactly?

Now we have the GetType operator. Let’s have GetMethod, GetProperty, GetEvent, GetField, GetParameter, GetValriable operators returning appropriate reflection objects like MethodInfo, PropertyInfo, EventInfo, FieldInfo, ParameterInfo, LocalVariableInfo.

Inspired by NameOf operator.

Why?

Because nowadays it must be obtained via reflection using name as string. It’s insecure because when you change name, it is not changed in string, code compiles, but does not work.

Possible limitations

I have not mentioned GetTypeParameter, because GetType can be used, but if there will be ability to obtain parameters of other than current method GetType must be enhanced or GetTypeParameter must be introduced. Maybe GetConstructor will be also useful as it returns ConstructorInfo.

Questions

What about operators? Either introduce GetOperator or enhance GetMethod to get operator.

How to deal with overloaded members? Same way as in proposal of enhanced references in XML comments (47; types in braces).

How to compile? It can be compiled using that reflection functions, but, maybe, CLI has more efficient way to do this provided by some instruction like ldtoken?

Examples

 

 

Proposed

 

Reflection equivalent

Comment

 

1

Class TestClass

 

2

Sub AnotherMethod(ByVal a As Integer)

 

3

End Sub

 

4

Sub AnotherMethod(ByVal a As Long)

 

5

End Sub

 

6

Property TestProp() As Char

 

7

Get

 

8

Dim a = GetMethod()

:

Dim a = GetType(TestClass).GetMethod("get_TestProp")

 

9

Dim b = GetProperty()

:

 Dim b = GetType(TestClass).GetProperty("TestProp")

 

10

End Get

 

11

Set(ByVal value As Char)

 

12

End Set

 

13

End Property

 

14

Private Shared Field As Long

 

15

Public Sub Test()

 

16

Dim xxx As Exception = New ArgumentNullException

 

17

Dim a = GetType(TestClass)

:

Dim a = Type.GetType("Namespace.TestClass")

'Works in 9

 

18

Dim b = GetType(MyClass)

:

 Dim b = Type.GetType("Namespace.TestClass")

'Works in shared methods as well

 

19

Dim c = GetType()

:

Dim c = Me.GetType()

'But works in shared methods as well

 

20

Dim d = GetMethod(TestClass.Test)

:

Dim d = GetType(TestClass).GetMethod("Test")

 

21

Dim e = GetMethod()

:

Dim e = GetType(TestClass).GetMethod("Test")

 

22

Dim f = GetMethod(Me.Test)

:

Dim f = Me.GetType.GetMethod("Test")

'To get actual override

 

23

Dim g = GetMethod(MyClass.Test)

:

Dim g = GetType(TestClass).GetMethod("Test")

'Works in shared methods as well

 

24

Dim h = GetMethod(MyBase.ToString)

:

Dim g = GetType(Object).GetMethod("ToString")

'Works in shared methods as well

 

25

Dim i = GetMethod(xxx.ToString)

:

Dim i = xxx.GetType.GetMethod("ToString")

'To get actual override

 

26

Dim j = GetMethod(Exception.ToString)

:

Dim j = GetType(Exception).GetMethod("ToString")

 

27

Dim k = GetMethod(TestClass.AnotherMethod)

'Should not compile, ambignous

 

28

Dim l = GetMethod(TestClass.AnotherMethod(Of Integer))

:

Dim l = GetType(TestClass).GetMethod("AnotherMethod", New Type() {GetType(Integer)})

 

29

Dim m = GetMethod(TestClass.TestProp.Get)

:

 Dim m = GetType(TestClass).GetProperty("TestProp").GetGetMethod

 

30

Dim n = GetProperty(TestProp)

:

Dim n = Me.GetType.GetProperty("TestProp")

 

31

Dim o = GetEvent(TestEvent)

:

Dim o = Me.GetType.GetEvent("TestEvent")

 

32

Dim p = GetMethod(TestCEvent.RaiseEvent)

:

Dim p = Me.GetType.GetEvent("TestCEvent").GetRaiseMethod

 

33

Dim q = GetMethod(TestEvent.RaiseEvent)

'Does not compile, designer-generated event does not have RaiseEvent accessors

 

34

Dim r = GetField(Field)

:

Dim r = GetType(TestClass).GetField("Field")

 

35

Dim s = GetParameter(TestClass.TestCEvent.RaiseEvent.sender)

:

Dim s = GetType(TestClass).GetEvent("TestCEvent").GetRaiseMethod.GetParameters()(0)

 

36

Dim t = GetParameter(AnotherMethod(Of Long).a)

:

 Dim t = GetType(TestClass).GetMethod("AnotherMethod", New Type() {GetType(Long)}).GetParameters()(0)

 

37

Dim u = GetVariable(xxx)

:

Dim u = GetType(TestClass).GetMethod("Test").GetMethodBody.LocalVariables(0)

 

38

Dim v = GetField(vbCrLf)

:

Dim v = GetType(Constants).GetField("vbCrLf")

 

39

Dim w = GetType(GenericClass)

:

Dim w = Type.GetType("Namespace.GenericClass")

'Works in 9

 

40

Dim x = GetType(GenericClass(Of ))

:

Dim x = Type.GetType("Namespace.GenericClass`1")

'Works in 9

 

41

Dim y = GetType(GenericClass(Of ,,))

:

Dim y = Type.GetType("Namespace.GenericClass`3")

'Works in 9

 

42

Dim z = GetType(GenericClass(Of Long))

:

Dim z = Type.GetType("Namespace.GenericClass`1").MakeGenericType(GetType(Long))

'Works in 9

 

43

Dim α = GetMethod(GenericMethod(Of ,))

:

Dim α  = GetType(TestClass).GetMethod("GenericMethod`2")

 

44

Dim β = GetMethod(GenericMethod(Of String, SByte))

:

Dim β = GetType(TestClass).GetMethod("GenericMethod`2").MakeGenericMethod(GetType(String), GetType(SByte))

 

45

Dim γ = GetMethod(GenericMethod(Of )(0))

:

Dim γ = GetType(TestClass).GetMethod("GenericMethod`1",New Type(){???})

'No idea how to do this easilly with refelection

 

46

Dim δ = GetMethod(GenericMethod(Of )(0, Object))

:

Dim δ = GetType(TestClass).GetMethod("GenericMethod`1", New Type(){???, GetType(Object)})

'No idea how to do this easilly with refelection

 

47

Dim ε = GetType(GenericClass(Of Integer ,,))

:

Dim ε = type.GetType("Namespace.GenericClass`3").MakeGenericType(GetType(Integer),type.GetType("Namespace.GenericClass`3").GetGenericArguments()(1),type.GetType("Namespace.GenericClass`3").GetGenericArguments()(2))

'Don't know if this is possible

 

48

Dim ζ = GetType(Long(,)())

:

Dim ζ = GetType(Long).MakeArrayType(2).MakeArrayType

'Works in 9

 

49

Dim η = GetType(ByRef Integer)

:

Dim η = GetType(Integer).MakeByRefType

 

50

Dim θ = GetType(Long?)

:

Dim θ = GetType(Nullable(Of )).MakeGenericType(GetType(Long))

'Works in 9

 

51

Dim ι = GetTypeParameter(GenericClass(Of ).T)

:

Dim ι = GetType(GenericClass(Of )).GetGenericArguments()(0)

 

52

Dim κ = GetTypeParameter(GenericMethod(Of ,).U)

:

Dim  κ = GetType(TestClass).GetMethod("GenericMethod`2").GetGenericArguments()(1)

 

53

Dim λ = GetOperator(+)

:

Dim λ = GetType (TestClass).GetMethod("op_Addition")

 

54

Dim μ = GetOperator(+(TestClass, TestClass))

:

Dim μ = GetType (TestClass).GetMethod("op_Addition", New Type(){GetType(TestClass),GetType(TestClass)})

 

55

Dim ν = GetMethod(TestClass.Operator +)

:

Dim ν = GetType(TestClass).GetMethod("op_Addition")

 

56

Dim ξ = GetOperator(TestClass.Widening CType(TestClass, As Exception))

:

Dim ξ = (From Method In GetType(TestClass).GetMethods Select Method Where Method.GetParameters.Length=1 AndAlso Method.GetParameters()(0).ParameterType.Equals(GetType(TestClass)) AndAlso Method.ReturnType.Equals(GetType(Exception))).First

'No idea for shorter expression

 

57

Dim ο = GetParameter(Operator +.b)

:

Dim ο = GetType(TestClass).GetMethod("op_Addition").GetParameters()(1)

 

58

Dim π = GetAssembly()

:

Dim π = GetType(TestClass).Assembly

 

59

Dim ρ = GetModule()

:

Dim ρ = GetType(TestClass).Module

 

60

End Sub

 

61

Public Custom Event TestCEvent As EventHandler

 

62

AddHandler(ByVal value As EventHandler)

 

63

End AddHandler

 

64

RemoveHandler(ByVal value As EventHandler)

 

65

End RemoveHandler

 

66

RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

 

67

Dim a = GetEvent()

:

Dim a = GetType(TestClass).GetEvent("TestCEvent")

 

68

Dim b = GetMethod()

:

Dim b = GetType(TestClass).GetMethod("raise_TestCEvent")

 

69

Dim c = GetParameter(sender)

:

Dim c = GetType(TestClass).GetMethod("raise_TestCEvent").GetParameters()(0)

 

70

End RaiseEvent

 

71

End Event

 

72

Public Event TestEvent As EventHandler

 

73

Public Sub GenericMethod(Of T, U)()

 

74

End Sub

 

75

Public Sub GenericMethod(Of T)(ByVal x As T)

 

76

End Sub

 

77

Public Sub GenericMethod(Of T)(ByVal x As T, ByVal more As Object)

 

78

End Sub

 

79

Public Shared Operator +(ByVal a As TestClass, ByVal b As TestClass) As TestClass

 

80

End Operator

 

81

Public Shared Widening Operator CType(ByVal a As TestClass) As Exception

 

82

End Operator

 

83

Public Shared Widening Operator CType(ByVal a As TestClass) As Attribute

 

84

End Operator

 

85

End Class

 

86

Class GenericClass 'This class is not generic

 

87

End Class

 

88

Class GenericClass(Of T)

 

89

Private Sub Test()

 

90

Dim a = GetType(T)

'Works in 9, gets actual type supplied for T

 

91

Dim b = GetTypeParameter(T)

:

Dim b = GetType(GenericClass(Of )).GetGenericArguments()(0)

 

92

End Sub

 

93

End Class

 

94

Class GenericClass(Of T, U, V)

 

95

End Class

 

96

Class LastTestClass

 

97

Shared Sub New()

 

98

End Sub

 

99

Public Sub New()

100

End Sub

101

Public Sub New(ByVal a As Long)

102

End Sub

103

Sub Test()

104

Dim a = GetMethod(New())

:

Dim a = GetType(LastTestClass).GetConstructor(New Type(){})

105

Dim b = GetMethod(New(Long))

:

Dim b = GetType(LastTestClass).GetConstructor(New Type(){GetType(Long)})

106

Dim c = GetMethod(New)

'Error - ambignous

107

Dim d = GetMethod(Shared New)

: