@@ -28,9 +28,9 @@ public static T ConvertTo<T>(this object from)
2828 if ( fromType == typeof ( T ) )
2929 return ( T ) from ;
3030
31- if ( fromType . IsValueType ( ) || typeof ( T ) . IsValueType ( ) )
31+ if ( fromType . IsValueType || typeof ( T ) . IsValueType )
3232 {
33- if ( ! fromType . IsEnum ( ) && ! typeof ( T ) . IsEnum ( ) )
33+ if ( ! fromType . IsEnum && ! typeof ( T ) . IsEnum )
3434 {
3535 if ( typeof ( T ) == typeof ( char ) && from is string s )
3636 return ( T ) ( s . Length > 0 ? ( object ) s [ 0 ] : null ) ;
@@ -58,7 +58,7 @@ public static T ConvertTo<T>(this object from)
5858 return ( T ) ChangeValueType ( from , typeof ( T ) ) ;
5959 }
6060
61- if ( typeof ( IEnumerable ) . IsAssignableFromType ( typeof ( T ) ) )
61+ if ( typeof ( IEnumerable ) . IsAssignableFrom ( typeof ( T ) ) )
6262 {
6363 var listResult = TranslateListWithElements . TryTranslateCollections (
6464 fromType , typeof ( T ) , from ) ;
@@ -72,10 +72,10 @@ public static T ConvertTo<T>(this object from)
7272
7373 public static T CreateCopy < T > ( this T from )
7474 {
75- if ( typeof ( T ) . IsValueType ( ) )
75+ if ( typeof ( T ) . IsValueType )
7676 return ( T ) ChangeValueType ( from , typeof ( T ) ) ;
7777
78- if ( typeof ( IEnumerable ) . IsAssignableFromType ( typeof ( T ) ) )
78+ if ( typeof ( IEnumerable ) . IsAssignableFrom ( typeof ( T ) ) )
7979 {
8080 var listResult = TranslateListWithElements . TryTranslateCollections (
8181 from . GetType ( ) , typeof ( T ) , from ) ;
@@ -101,10 +101,10 @@ public static object ConvertTo(this object from, Type type)
101101 if ( from . GetType ( ) == type )
102102 return from ;
103103
104- if ( from . GetType ( ) . IsValueType ( ) || type . IsValueType ( ) )
104+ if ( from . GetType ( ) . IsValueType || type . IsValueType )
105105 return ChangeValueType ( from , type ) ;
106106
107- if ( typeof ( IEnumerable ) . IsAssignableFromType ( type ) )
107+ if ( typeof ( IEnumerable ) . IsAssignableFrom ( type ) )
108108 {
109109 var listResult = TranslateListWithElements . TryTranslateCollections (
110110 from . GetType ( ) , type , from ) ;
@@ -129,7 +129,7 @@ private static object ChangeValueType(object from, Type type)
129129
130130 public static object ChangeTo ( this string strValue , Type type )
131131 {
132- if ( type . IsValueType ( ) && ! type . IsEnum ( ) && type . HasInterface ( typeof ( IConvertible ) ) )
132+ if ( type . IsValueType && ! type . IsEnum && type . HasInterface ( typeof ( IConvertible ) ) )
133133 {
134134 try
135135 {
@@ -177,14 +177,14 @@ public static bool IsDebugBuild(this Assembly assembly)
177177 public static object PopulateWith ( object obj )
178178 {
179179 if ( obj == null ) return null ;
180- var isHttpResult = obj . GetType ( ) . Interfaces ( ) . Any ( x => x . Name == "IHttpResult" ) ; // No coupling FTW!
180+ var isHttpResult = obj . GetType ( ) . GetInterfaces ( ) . Any ( x => x . Name == "IHttpResult" ) ; // No coupling FTW!
181181 if ( isHttpResult )
182182 {
183183 obj = new CustomHttpResult ( ) ;
184184 }
185185
186186 var type = obj . GetType ( ) ;
187- if ( type . IsArray ( ) || type . IsValueType ( ) || type . IsGeneric ( ) )
187+ if ( type . IsArray || type . IsValueType || type . IsGenericType )
188188 {
189189 var value = CreateDefaultValue ( type , new Dictionary < Type , int > ( 20 ) ) ;
190190 return value ;
@@ -224,19 +224,18 @@ private static object PopulateObjectInternal(object obj, Dictionary<Type, int> r
224224
225225 public static object GetDefaultValue ( this Type type )
226226 {
227- if ( ! type . IsValueType ( ) ) return null ;
227+ if ( ! type . IsValueType ) return null ;
228228
229- object defaultValue ;
230- if ( DefaultValueTypes . TryGetValue ( type , out defaultValue ) ) return defaultValue ;
229+ if ( DefaultValueTypes . TryGetValue ( type , out var defaultValue ) )
230+ return defaultValue ;
231231
232232 defaultValue = Activator . CreateInstance ( type ) ;
233233
234234 Dictionary < Type , object > snapshot , newCache ;
235235 do
236236 {
237237 snapshot = DefaultValueTypes ;
238- newCache = new Dictionary < Type , object > ( DefaultValueTypes ) ;
239- newCache [ type ] = defaultValue ;
238+ newCache = new Dictionary < Type , object > ( DefaultValueTypes ) { [ type] = defaultValue } ;
240239
241240 } while ( ! ReferenceEquals (
242241 Interlocked . CompareExchange ( ref DefaultValueTypes , newCache , snapshot ) , snapshot ) ) ;
@@ -304,7 +303,7 @@ private static Dictionary<string, AssignmentMember> GetMembers(Type type, bool i
304303 }
305304 else
306305 {
307- if ( propertyInfo . CanWrite && propertyInfo . SetMethod ( ) != null )
306+ if ( propertyInfo . CanWrite && propertyInfo . GetSetMethod ( nonPublic : true ) != null )
308307 {
309308 map [ info . Name ] = new AssignmentMember ( propertyInfo . PropertyType , propertyInfo ) ;
310309 continue ;
@@ -419,7 +418,7 @@ public static void SetProperty(this PropertyInfo propertyInfo, object obj, objec
419418 return ;
420419 }
421420
422- var propertySetMetodInfo = propertyInfo . SetMethod ( ) ;
421+ var propertySetMetodInfo = propertyInfo . GetSetMethod ( nonPublic : true ) ;
423422 if ( propertySetMetodInfo != null )
424423 {
425424 propertySetMetodInfo . Invoke ( obj , new [ ] { value } ) ;
@@ -431,7 +430,7 @@ public static object GetProperty(this PropertyInfo propertyInfo, object obj)
431430 if ( propertyInfo == null || ! propertyInfo . CanRead )
432431 return null ;
433432
434- var getMethod = propertyInfo . GetMethodInfo ( ) ;
433+ var getMethod = propertyInfo . GetGetMethod ( nonPublic : true ) ;
435434 return getMethod != null ? getMethod . Invoke ( obj , TypeConstants . EmptyObjectArray ) : null ;
436435 }
437436
@@ -462,7 +461,7 @@ public static bool IsUnsettableValue(FieldInfo fieldInfo, PropertyInfo propertyI
462461 // Currently we define those properties as properties declared on
463462 // types defined in mscorlib
464463
465- if ( propertyInfo != null && propertyInfo . ReflectedType ( ) != null )
464+ if ( propertyInfo != null && propertyInfo . ReflectedType != null )
466465 {
467466 return PclExport . Instance . InSameAssembly ( propertyInfo . DeclaringType , typeof ( object ) ) ;
468467 }
@@ -489,32 +488,31 @@ public static object CreateDefaultValue(Type type, Dictionary<Type, int> recursi
489488 return type . Name ;
490489 }
491490
492- if ( type . IsEnum ( ) )
491+ if ( type . IsEnum )
493492 {
494493 return Enum . GetValues ( type ) . GetValue ( 0 ) ;
495494 }
496495
497- if ( type . IsAbstract ( ) )
496+ if ( type . IsAbstract )
498497 return null ;
499498
500499 // If we have hit our recursion limit for this type, then return null
501- int recurseLevel ; // will get set to 0 if TryGetValue() fails
502- recursionInfo . TryGetValue ( type , out recurseLevel ) ;
500+ recursionInfo . TryGetValue ( type , out var recurseLevel ) ;
503501 if ( recurseLevel > MaxRecursionLevelForDefaultValues ) return null ;
504502
505503 recursionInfo [ type ] = recurseLevel + 1 ; // increase recursion level for this type
506504 try // use a try/finally block to make sure we decrease the recursion level for this type no matter which code path we take,
507505 {
508506
509507 //when using KeyValuePair<TKey, TValue>, TKey must be non-default to stuff in a Dictionary
510- if ( type . IsGeneric ( ) && type . GenericTypeDefinition ( ) == typeof ( KeyValuePair < , > ) )
508+ if ( type . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( KeyValuePair < , > ) )
511509 {
512- var genericTypes = type . GenericTypeArguments ( ) ;
510+ var genericTypes = type . GetGenericArguments ( ) ;
513511 var valueType = Activator . CreateInstance ( type , CreateDefaultValue ( genericTypes [ 0 ] , recursionInfo ) , CreateDefaultValue ( genericTypes [ 1 ] , recursionInfo ) ) ;
514512 return PopulateObjectInternal ( valueType , recursionInfo ) ;
515513 }
516514
517- if ( type . IsValueType ( ) )
515+ if ( type . IsValueType )
518516 {
519517 return type . CreateInstance ( ) ;
520518 }
@@ -524,7 +522,7 @@ public static object CreateDefaultValue(Type type, Dictionary<Type, int> recursi
524522 return PopulateArray ( type , recursionInfo ) ;
525523 }
526524
527- var constructorInfo = type . GetEmptyConstructor ( ) ;
525+ var constructorInfo = type . GetConstructor ( Type . EmptyTypes ) ;
528526 var hasEmptyConstructor = constructorInfo != null ;
529527
530528 if ( hasEmptyConstructor )
@@ -550,7 +548,7 @@ public static object CreateDefaultValue(Type type, Dictionary<Type, int> recursi
550548
551549 public static void SetGenericCollection ( Type realisedListType , object genericObj , Dictionary < Type , int > recursionInfo )
552550 {
553- var args = realisedListType . GenericTypeArguments ( ) ;
551+ var args = realisedListType . GetGenericArguments ( ) ;
554552 if ( args . Length != 1 )
555553 {
556554 Tracer . Instance . WriteError ( "Found a generic list that does not take one generic argument: {0}" , realisedListType ) ;
@@ -580,9 +578,9 @@ public static Array PopulateArray(Type type, Dictionary<Type, int> recursionInfo
580578 //TODO: replace with InAssignableFrom
581579 public static bool CanCast ( Type toType , Type fromType )
582580 {
583- if ( toType . IsInterface ( ) )
581+ if ( toType . IsInterface )
584582 {
585- var interfaceList = fromType . Interfaces ( ) . ToList ( ) ;
583+ var interfaceList = fromType . GetInterfaces ( ) . ToList ( ) ;
586584 if ( interfaceList . Contains ( toType ) ) return true ;
587585 }
588586 else
@@ -593,7 +591,7 @@ public static bool CanCast(Type toType, Type fromType)
593591 {
594592 areSameTypes = baseType == toType ;
595593 }
596- while ( ! areSameTypes && ( baseType = fromType . BaseType ( ) ) != null ) ;
594+ while ( ! areSameTypes && ( baseType = fromType . BaseType ) != null ) ;
597595
598596 if ( areSameTypes ) return true ;
599597 }
@@ -617,7 +615,7 @@ public static IEnumerable<KeyValuePair<PropertyInfo, T>> GetPropertyAttributes<T
617615 }
618616 }
619617 }
620- while ( ( baseType = baseType . BaseType ( ) ) != null ) ;
618+ while ( ( baseType = baseType . BaseType ) != null ) ;
621619 }
622620 }
623621
@@ -799,15 +797,15 @@ public static GetMemberDelegate CreateTypeConverter(Type fromType, Type toType)
799797 var underlyingToType = Nullable . GetUnderlyingType ( toType ) ?? toType ;
800798 var underlyingFromType = Nullable . GetUnderlyingType ( fromType ) ?? fromType ;
801799
802- if ( underlyingToType . IsEnum ( ) )
800+ if ( underlyingToType . IsEnum )
803801 {
804- if ( underlyingFromType . IsEnum ( ) || fromType == typeof ( string ) )
802+ if ( underlyingFromType . IsEnum || fromType == typeof ( string ) )
805803 return fromValue => Enum . Parse ( underlyingToType , fromValue . ToString ( ) , ignoreCase : true ) ;
806804
807805 if ( underlyingFromType . IsIntegerType ( ) )
808806 return fromValue => Enum . ToObject ( underlyingToType , fromValue ) ;
809807 }
810- else if ( underlyingFromType . IsEnum ( ) )
808+ else if ( underlyingFromType . IsEnum )
811809 {
812810 if ( underlyingToType . IsIntegerType ( ) )
813811 return fromValue => Convert . ChangeType ( fromValue , underlyingToType , null ) ;
@@ -816,7 +814,7 @@ public static GetMemberDelegate CreateTypeConverter(Type fromType, Type toType)
816814 {
817815 return null ;
818816 }
819- else if ( typeof ( IEnumerable ) . IsAssignableFromType ( fromType ) )
817+ else if ( typeof ( IEnumerable ) . IsAssignableFrom ( fromType ) )
820818 {
821819 return fromValue =>
822820 {
@@ -826,7 +824,7 @@ public static GetMemberDelegate CreateTypeConverter(Type fromType, Type toType)
826824 return listResult ?? fromValue ;
827825 } ;
828826 }
829- else if ( toType . IsValueType ( ) )
827+ else if ( toType . IsValueType )
830828 {
831829 return fromValue => Convert . ChangeType ( fromValue , toType , provider : null ) ;
832830 }
0 commit comments