@@ -28,6 +28,11 @@ public static T CreateInstance<
28
28
#endif
29
29
T > ( params object ? [ ] args )
30
30
{
31
+ if ( typeof ( T ) . IsValueType && ( args == null || args . Length == 0 ) )
32
+ {
33
+ return default ! ;
34
+ }
35
+
31
36
return ( T ) CreateInstance ( GetRequiredType < T > ( ) , args ) ;
32
37
}
33
38
@@ -50,10 +55,27 @@ public static object CreateInstance(
50
55
{
51
56
args ??= [ ] ;
52
57
53
- var methods = typeInfo . DeclaredConstructors . Where ( x => x . Parameters . Count ( x => ! x . HasDefaultValue ) <= args . Length ) . ToList ( ) ;
58
+ if ( typeInfo . Type . IsValueType )
59
+ {
60
+ if ( typeInfo . IsReflected )
61
+ {
62
+ return Activator . CreateInstance ( typeInfo . Type , args ) ! ;
63
+ }
64
+
65
+ if ( args . Length == 0 )
66
+ {
67
+ var ctor = typeInfo . DeclaredConstructors . FirstOrDefault ( x => x . Parameters . Length == 0 && x . Accessibility == SourceAccessibility . Public ) ;
68
+ if ( ctor != null )
69
+ {
70
+ return ctor . Invoke ( args ) ! ;
71
+ }
72
+ }
73
+ }
54
74
55
- var method = Type . DefaultBinder . BindToMethod ( ReflectionExtensions . DeclaredOnlyLookup , methods . Select ( x => x . ConstructorInfo ) . ToArray ( ) , ref args , null , null , null , out object ? state ) ;
56
75
76
+
77
+ var methods = typeInfo . DeclaredConstructors . Where ( x => x . Parameters . Count ( x => ! x . HasDefaultValue ) <= args . Length ) . ToList ( ) ;
78
+ var method = Type . DefaultBinder . BindToMethod ( ReflectionExtensions . DeclaredOnlyLookup , methods . Select ( x => x . ConstructorInfo ) . ToArray ( ) , ref args , null , null , null , out _ ) ;
57
79
if ( typeInfo . IsReflected )
58
80
{
59
81
return method . Invoke ( null , args ) ! ;
@@ -114,10 +136,7 @@ public static SourceTypeInfo? GetType<
114
136
115
137
if ( allowRuntimeReflection )
116
138
{
117
- if ( allowRuntimeReflection )
118
- {
119
- return _reflectionTypes . GetOrAdd ( type , CreateSourceTypeInfo ) ;
120
- }
139
+ return _reflectionTypes . GetOrAdd ( type , CreateSourceTypeInfo ) ;
121
140
}
122
141
return null ;
123
142
}
0 commit comments