@@ -487,7 +487,7 @@ public static object CreateInstance(this Type type, BindingFlags flags, params o
487487
488488 public static object CreateInstance ( this Type type , IHostInvokeContext invokeContext , HostTarget target , object [ ] args , object [ ] bindArgs )
489489 {
490- if ( type . IsCOMObject || ( type . IsValueType && ( args . Length < 1 ) ) )
490+ if ( type . IsCOMObject )
491491 {
492492 return type . CreateInstance ( args ) ;
493493 }
@@ -500,7 +500,13 @@ public static object CreateInstance(this Type type, IHostInvokeContext invokeCon
500500 return InvokeHelpers . InvokeConstructor ( invokeContext , boundConstructor , args ) ;
501501 }
502502
503- var candidates = type . GetConstructors ( flags ) . Where ( testConstructor => testConstructor . IsAccessible ( invokeContext . AccessContext ) && ! testConstructor . IsBlockedFromScript ( invokeContext . DefaultAccess ) ) . ToArray ( ) ;
503+ var constructors = type . GetConstructors ( flags ) ;
504+ if ( type . IsValueType && ( args . Length < 1 ) && ! constructors . Any ( testConstructor => testConstructor . GetParameters ( ) . Length < 1 ) )
505+ {
506+ return type . CreateInstance ( ) ;
507+ }
508+
509+ var candidates = constructors . Where ( testConstructor => testConstructor . IsAccessible ( invokeContext . AccessContext ) && ! testConstructor . IsBlockedFromScript ( invokeContext . DefaultAccess ) ) . ToArray ( ) ;
504510 if ( candidates . Length < 1 )
505511 {
506512 throw new MissingMethodException ( MiscHelpers . FormatInvariant ( "Type '{0}' has no constructor that matches the specified arguments" , type . GetFullFriendlyName ( ) ) ) ;
@@ -1271,14 +1277,16 @@ public int CompareTo(BindArgCost other)
12711277
12721278 private sealed class BindCandidate < T > : IComparable < BindCandidate < T > > where T : MemberInfo
12731279 {
1280+ private readonly int defaultArgCount ;
12741281 private readonly bool paramArray ;
12751282 private readonly List < BindArgCost > argCosts ;
12761283
12771284 public T Candidate { get ; }
12781285
1279- private BindCandidate ( T candidate , bool paramArray , List < BindArgCost > argCosts )
1286+ private BindCandidate ( T candidate , int defaultArgCount , bool paramArray , List < BindArgCost > argCosts )
12801287 {
12811288 Candidate = candidate ;
1289+ this . defaultArgCount = defaultArgCount ;
12821290 this . paramArray = paramArray ;
12831291 this . argCosts = argCosts ;
12841292 }
@@ -1297,6 +1305,12 @@ public int CompareTo(BindCandidate<T> other)
12971305 }
12981306 }
12991307
1308+ result = defaultArgCount . CompareTo ( other . defaultArgCount ) ;
1309+ if ( result != 0 )
1310+ {
1311+ return result ;
1312+ }
1313+
13001314 result = paramArray . CompareTo ( other . paramArray ) ;
13011315 if ( result != 0 )
13021316 {
@@ -1316,6 +1330,7 @@ public int CompareTo(BindCandidate<T> other)
13161330
13171331 public static BindCandidate < T > TryCreateInstance ( T candidate , ParameterInfo [ ] parameters , object [ ] args , Type [ ] argTypes )
13181332 {
1333+ var defaultArgCount = 0 ;
13191334 var paramArray = false ;
13201335 var argCosts = new List < BindArgCost > ( ) ;
13211336
@@ -1343,6 +1358,7 @@ public static BindCandidate<T> TryCreateInstance(T candidate, ParameterInfo[] pa
13431358 return null ;
13441359 }
13451360
1361+ defaultArgCount += 1 ;
13461362 continue ;
13471363 }
13481364
@@ -1359,13 +1375,13 @@ public static BindCandidate<T> TryCreateInstance(T candidate, ParameterInfo[] pa
13591375 {
13601376 if ( argIndex >= args . Length )
13611377 {
1362- return new BindCandidate < T > ( candidate , true , argCosts ) ;
1378+ return new BindCandidate < T > ( candidate , defaultArgCount , true , argCosts ) ;
13631379 }
13641380
13651381 if ( ( argIndex == ( args . Length - 1 ) ) && paramType . IsBindableFromArg ( args [ argIndex ] , argTypes [ argIndex ] , out cost ) )
13661382 {
13671383 argCosts . Add ( cost ) ;
1368- return new BindCandidate < T > ( candidate , true , argCosts ) ;
1384+ return new BindCandidate < T > ( candidate , defaultArgCount , true , argCosts ) ;
13691385 }
13701386
13711387 paramType = paramType . GetElementType ( ) ;
@@ -1385,7 +1401,7 @@ public static BindCandidate<T> TryCreateInstance(T candidate, ParameterInfo[] pa
13851401 return null ;
13861402 }
13871403
1388- return new BindCandidate < T > ( candidate , paramArray , argCosts ) ;
1404+ return new BindCandidate < T > ( candidate , defaultArgCount , paramArray , argCosts ) ;
13891405 }
13901406 }
13911407
0 commit comments