1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
4
+ using System . Diagnostics ;
3
5
using System . Globalization ;
4
6
#if ! NETSTANDARD1_3
5
7
using System . Dynamic ;
@@ -399,7 +401,32 @@ protected override Expression VisitBinary(BinaryExpression node)
399
401
return node . Update ( left , node . Conversion , right ) ;
400
402
}
401
403
404
+ private byte ? _nilCtx ;
405
+ private byte [ ] ? _nil ;
406
+ private int _nilIndex ;
407
+ private string TranslateNullable ( Type type , byte ? nullableContext , byte [ ] ? nullable )
408
+ {
409
+ try
410
+ {
411
+ _nilCtx = nullableContext ;
412
+ _nil = nullable ;
413
+ _nilIndex = 0 ;
414
+ return Translate ( type ) ;
415
+ }
416
+ finally
417
+ {
418
+ _nilCtx = null ;
419
+ _nil = null ;
420
+ }
421
+ }
402
422
public string Translate ( Type type )
423
+ {
424
+ var refNullable = ! type . GetTypeInfo ( ) . IsValueType &&
425
+ ( _nilCtx == 2 || _nilIndex < _nil ? . Length && _nil [ _nilIndex ++ ] == 2 ) ;
426
+ var typeName = TranslateInner ( type ) ;
427
+ return refNullable ? $ "{ typeName } ?" : typeName ;
428
+ }
429
+ private string TranslateInner ( Type type )
403
430
{
404
431
if ( type == typeof ( bool ) )
405
432
return "bool" ;
@@ -452,7 +479,15 @@ public string Translate(Type type)
452
479
return Translate ( underlyingType ) + "?" ;
453
480
454
481
_usings ??= new HashSet < string > ( ) ;
455
- if ( ! this . TypeNames . TryGetValue ( type , out var name ) )
482
+
483
+ string name ;
484
+ if ( _nilCtx != null || _nil != null )
485
+ {
486
+ name = GetTypeName ( type ) ;
487
+ if ( Definitions ? . PrintFullTypeName != true && ! string . IsNullOrEmpty ( type . Namespace ) )
488
+ _usings . Add ( type . Namespace ) ;
489
+ }
490
+ else if ( ! this . TypeNames . TryGetValue ( type , out name ) )
456
491
{
457
492
name = GetTypeName ( type ) ;
458
493
@@ -461,6 +496,8 @@ public string Translate(Type type)
461
496
var count = this . TypeNames . Count ( kvp => GetTypeName ( kvp . Key ) == name ) ;
462
497
if ( count > 0 )
463
498
{
499
+ // NOTE: type alias cannot solve all name conflicted case, user should use PrintFullTypeName
500
+ // keep logic here for compatability
464
501
if ( ! type . GetTypeInfo ( ) . IsGenericType )
465
502
name += count + 1 ;
466
503
else if ( ! string . IsNullOrEmpty ( type . Namespace ) )
@@ -489,7 +526,7 @@ private string GetTypeName(Type type)
489
526
if ( type . DeclaringType == null )
490
527
return name ;
491
528
492
- return Translate ( type . DeclaringType ) + "." + name ;
529
+ return TranslateInner ( type . DeclaringType ) + "." + name ;
493
530
}
494
531
495
532
private string GetSingleTypeName ( Type type )
@@ -1841,9 +1878,8 @@ public override string ToString()
1841
1878
. ToList ( ) ;
1842
1879
var properties = _properties ?
1843
1880
. ToDictionary ( it => it . Name ,
1844
- it => $ "{ Translate ( it . Type ) } { it . Name } {{ get; { ( it . IsReadOnly ? "" : it . IsInitOnly ? "init; " : "set; " ) } }}") ;
1881
+ it => $ "{ TranslateNullable ( it . Type , it . NullableContext ?? Definitions ? . NullableContext , it . Nullable ) } { it . Name } {{ get; { ( it . IsReadOnly ? "" : it . IsInitOnly ? "init; " : "set; " ) } }}") ;
1845
1882
var ctorParams = _properties ? . Where ( it => it . IsReadOnly ) . ToList ( ) ;
1846
-
1847
1883
if ( Definitions ? . TypeName != null )
1848
1884
{
1849
1885
if ( _usings != null )
@@ -1858,6 +1894,9 @@ public override string ToString()
1858
1894
}
1859
1895
WriteLine ( ) ;
1860
1896
}
1897
+
1898
+ // NOTE: type alias cannot solve all name conflicted case, user should use PrintFullTypeName
1899
+ // keep logic here for compatability
1861
1900
if ( _typeNames != null )
1862
1901
{
1863
1902
var names = _typeNames
@@ -1871,6 +1910,7 @@ public override string ToString()
1871
1910
if ( names . Count > 0 )
1872
1911
WriteLine ( ) ;
1873
1912
}
1913
+
1874
1914
if ( Definitions . Namespace != null )
1875
1915
{
1876
1916
WriteNextLine ( "namespace " , Definitions . Namespace ) ;
@@ -1972,7 +2012,7 @@ private void WriteCtorParams(List<PropertyDefinitions> ctorParams)
1972
2012
var parameter = ctorParams [ i ] ;
1973
2013
if ( i > 0 )
1974
2014
Write ( ", " ) ;
1975
- Write ( $ "{ Translate ( parameter . Type ) } { char . ToLower ( parameter . Name [ 0 ] ) + parameter . Name . Substring ( 1 ) } ") ;
2015
+ Write ( $ "{ TranslateNullable ( parameter . Type , parameter . NullableContext ?? Definitions ? . NullableContext , parameter . Nullable ) } { char . ToLower ( parameter . Name [ 0 ] ) + parameter . Name . Substring ( 1 ) } ") ;
1976
2016
}
1977
2017
Write ( ")" ) ;
1978
2018
}
0 commit comments