@@ -2453,23 +2453,49 @@ private void GenerateNativeConstructor(Class @class)
2453
2453
2454
2454
if ( @class . IsRefType )
2455
2455
{
2456
+ if ( @class . IsSingleton )
2457
+ {
2458
+ WriteLine ( $ "private static { printedClass } singletonInstance;") ;
2459
+ }
2460
+
2456
2461
var @new = @class . HasBase && @class . HasRefBase ( ) ;
2457
2462
2458
2463
bool generateNativeToManaged = Options . GenerateNativeToManagedFor ( @class ) ;
2459
2464
if ( generateNativeToManaged )
2460
2465
{
2461
- WriteLines ( $@ "
2466
+ if ( @class . IsSingleton )
2467
+ {
2468
+ WriteLines ( $@ "
2462
2469
internal static{ ( @new ? " new" : string . Empty ) } { printedClass } __GetOrCreateInstance({ TypePrinter . IntPtrType } native, bool saveInstance = false, bool skipVTables = false)
2463
2470
{{
2464
2471
if (native == { TypePrinter . IntPtrType } .Zero)
2465
2472
return null;
2473
+ if (singletonInstance != null)
2474
+ return singletonInstance;
2466
2475
if ({ Helpers . TryGetNativeToManagedMappingIdentifier } (native, out var managed))
2467
2476
return ({ printedClass } )managed;
2468
2477
var result = { Helpers . CreateInstanceIdentifier } (native, skipVTables);
2478
+ singletonInstance = result;
2469
2479
if (saveInstance)
2470
2480
{ Helpers . RecordNativeToManagedMappingIdentifier } (native, result);
2471
2481
return result;
2472
2482
}}" ) ;
2483
+ }
2484
+ else
2485
+ {
2486
+ WriteLines ( $@ "
2487
+ internal static{ ( @new ? " new" : string . Empty ) } { printedClass } __GetOrCreateInstance({ TypePrinter . IntPtrType } native, bool saveInstance = false, bool skipVTables = false)
2488
+ {{
2489
+ if (native == { TypePrinter . IntPtrType } .Zero)
2490
+ return null;
2491
+ if ({ Helpers . TryGetNativeToManagedMappingIdentifier } (native, out var managed))
2492
+ return ({ printedClass } )managed;
2493
+ var result = { Helpers . CreateInstanceIdentifier } (native, skipVTables);
2494
+ if (saveInstance)
2495
+ { Helpers . RecordNativeToManagedMappingIdentifier } (native, result);
2496
+ return result;
2497
+ }}" ) ;
2498
+ }
2473
2499
NewLine ( ) ;
2474
2500
}
2475
2501
@@ -2481,7 +2507,6 @@ private void GenerateNativeConstructor(Class @class)
2481
2507
if ( @class . IsSingleton )
2482
2508
{
2483
2509
WriteLines ( $@ "
2484
- private static { printedClass } singletonInstance;
2485
2510
internal static{ ( @new ? " new" : string . Empty ) } { printedClass } __GetInstance({ TypePrinter . IntPtrType } native)
2486
2511
{{
2487
2512
if (singletonInstance != null)
0 commit comments