1
+ using System ;
2
+ using System . Reflection ;
3
+ using NHibernate . Mapping . ByCode ;
4
+ using NUnit . Framework ;
5
+ using SharpTestsEx ;
6
+
7
+ namespace NHibernate . Test . MappingByCode . TypeExtensionsTests
8
+ {
9
+ public class GetMemberFromReflectedTest
10
+ {
11
+ private const BindingFlags PrivateMembersFlags = BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . DeclaredOnly ;
12
+
13
+ private class MyClass
14
+ {
15
+ private int pField ;
16
+ private int PrivateProperty { get ; set ; }
17
+ public int AnotherProperty { get ; set ; }
18
+ protected int ProtectedProperty { get ; set ; }
19
+ private int Method ( ) { return 0 ; }
20
+ }
21
+
22
+ private class Inherited : MyClass
23
+ {
24
+ private int pField ;
25
+ private int PrivateProperty { get ; set ; }
26
+ }
27
+
28
+ private interface IInterface
29
+ {
30
+ int Something { get ; set ; }
31
+ int SomethingElse { get ; set ; }
32
+ }
33
+
34
+ private class MyClassWithExplicitImpl : IInterface
35
+ {
36
+ int IInterface . Something
37
+ {
38
+ get
39
+ {
40
+ throw new System . NotImplementedException ( ) ;
41
+ }
42
+ set
43
+ {
44
+ throw new System . NotImplementedException ( ) ;
45
+ }
46
+ }
47
+
48
+ public int SomethingElse
49
+ {
50
+ get { throw new NotImplementedException ( ) ; }
51
+ set { throw new NotImplementedException ( ) ; }
52
+ }
53
+ }
54
+
55
+ [ Test ]
56
+ public void WhenNullMemberThenThrows ( )
57
+ {
58
+ Executing . This ( ( ) => ( ( MemberInfo ) null ) . GetMemberFromReflectedType ( typeof ( MyClass ) ) ) . Should ( ) . Throw < ArgumentNullException > ( ) ;
59
+ }
60
+
61
+ [ Test ]
62
+ public void WhenNullTypeThenThrows ( )
63
+ {
64
+ Executing . This ( ( ) => typeof ( MyClassWithExplicitImpl ) . GetProperty ( "SomethingElse" ) . GetMemberFromReflectedType ( null ) ) . Should ( ) . Throw < ArgumentNullException > ( ) ;
65
+ }
66
+
67
+ [ Test ]
68
+ public void WhenNotExistentThenOriginal ( )
69
+ {
70
+ var propertyInfo = typeof ( MyClassWithExplicitImpl ) . GetProperty ( "SomethingElse" ) ;
71
+ propertyInfo . GetMemberFromReflectedType ( typeof ( object ) ) . Should ( ) . Be ( propertyInfo ) ;
72
+ }
73
+
74
+ [ Test ]
75
+ public void WhenNotAccessibleFieldThenOriginal ( )
76
+ {
77
+ var memberInfo = typeof ( MyClass ) . GetField ( "pField" , PrivateMembersFlags ) ;
78
+ memberInfo . GetMemberFromReflectedType ( typeof ( Inherited ) ) . Should ( ) . Be ( memberInfo ) ;
79
+ }
80
+
81
+ [ Test ]
82
+ public void WhenNotAccessiblePropertyThenOriginal ( )
83
+ {
84
+ var memberInfo = typeof ( MyClass ) . GetProperty ( "PrivateProperty" , PrivateMembersFlags ) ;
85
+ memberInfo . GetMemberFromReflectedType ( typeof ( Inherited ) ) . Should ( ) . Be ( memberInfo ) ;
86
+ }
87
+
88
+ [ Test ]
89
+ public void WhenAccessiblePropertyThenReflected ( )
90
+ {
91
+ var memberInfo = typeof ( MyClass ) . GetProperty ( "ProtectedProperty" , PrivateMembersFlags ) ;
92
+ var result = memberInfo . GetMemberFromReflectedType ( typeof ( Inherited ) ) ;
93
+ result . ReflectedType . Should ( ) . Be ( typeof ( Inherited ) ) ;
94
+ result . DeclaringType . Should ( ) . Be ( typeof ( MyClass ) ) ;
95
+ }
96
+
97
+ [ Test ]
98
+ public void WhenPrivateFieldOnInheritedThenFindItOnInherited ( )
99
+ {
100
+ var memberInfo = typeof ( Inherited ) . GetField ( "pField" , PrivateMembersFlags ) ;
101
+ var result = memberInfo . GetMemberFromReflectedType ( typeof ( MyClass ) ) ;
102
+ result . ReflectedType . Should ( ) . Be ( typeof ( Inherited ) ) ;
103
+ result . DeclaringType . Should ( ) . Be ( typeof ( Inherited ) ) ;
104
+ }
105
+
106
+ [ Test ]
107
+ public void WhenPublicPropertyOfBaseOnInheritedThenFindItOnInherited ( )
108
+ {
109
+ var memberInfo = typeof ( MyClass ) . GetProperty ( "AnotherProperty" ) ;
110
+ var result = memberInfo . GetMemberFromReflectedType ( typeof ( Inherited ) ) ;
111
+ result . ReflectedType . Should ( ) . Be ( typeof ( Inherited ) ) ;
112
+ result . DeclaringType . Should ( ) . Be ( typeof ( MyClass ) ) ;
113
+ }
114
+
115
+ [ Test ]
116
+ public void WhenPropertyOfInterfaceThenFindItOnClass ( )
117
+ {
118
+ var memberInfo = typeof ( IInterface ) . GetProperty ( "SomethingElse" ) ;
119
+ var result = memberInfo . GetMemberFromReflectedType ( typeof ( MyClassWithExplicitImpl ) ) ;
120
+ result . DeclaringType . Should ( ) . Be ( typeof ( MyClassWithExplicitImpl ) ) ;
121
+ result . ReflectedType . Should ( ) . Be ( typeof ( MyClassWithExplicitImpl ) ) ;
122
+ }
123
+
124
+ [ Test ]
125
+ public void WhenPropertyOfExplicitInterfaceThenFindItOnClass ( )
126
+ {
127
+ var memberInfo = typeof ( IInterface ) . GetProperty ( "Something" ) ;
128
+ var result = memberInfo . GetMemberFromReflectedType ( typeof ( MyClassWithExplicitImpl ) ) ;
129
+ result . DeclaringType . Should ( ) . Be ( typeof ( MyClassWithExplicitImpl ) ) ;
130
+ result . ReflectedType . Should ( ) . Be ( typeof ( MyClassWithExplicitImpl ) ) ;
131
+ }
132
+ }
133
+ }
0 commit comments