@@ -296,6 +296,35 @@ private static bool IsUnsafe(ITypeSymbol symbol) =>
296
296
private static string EscapeIdentifier ( string identifier ) =>
297
297
keywords . Contains ( identifier ) ? "@" + identifier : identifier ;
298
298
299
+ private static bool TryGetConstantValue ( IFieldSymbol symbol , out string value )
300
+ {
301
+ value = "" ;
302
+ if ( ! symbol . HasConstantValue )
303
+ {
304
+ return false ;
305
+ }
306
+
307
+ var v = symbol . ConstantValue ;
308
+ switch ( symbol . Type . SpecialType )
309
+ {
310
+ case SpecialType . System_Boolean :
311
+ value = ( bool ) v ? "true" : "false" ;
312
+ return true ;
313
+ case SpecialType . System_Byte :
314
+ case SpecialType . System_SByte :
315
+ case SpecialType . System_UInt16 :
316
+ case SpecialType . System_UInt32 :
317
+ case SpecialType . System_UInt64 :
318
+ case SpecialType . System_Int16 :
319
+ case SpecialType . System_Int32 :
320
+ case SpecialType . System_Int64 :
321
+ value = v . ToString ( ) ! ;
322
+ return true ;
323
+ default :
324
+ return false ;
325
+ }
326
+ }
327
+
299
328
public override void VisitField ( IFieldSymbol symbol )
300
329
{
301
330
if ( IsNotPublic ( symbol . DeclaredAccessibility ) )
@@ -325,7 +354,8 @@ public override void VisitField(IFieldSymbol symbol)
325
354
stubWriter . Write ( EscapeIdentifier ( symbol . Name ) ) ;
326
355
if ( symbol . IsConst )
327
356
{
328
- stubWriter . Write ( " = default" ) ;
357
+ var v = TryGetConstantValue ( symbol , out var value ) ? value : "default" ;
358
+ stubWriter . Write ( $ " = { v } ") ;
329
359
}
330
360
stubWriter . WriteLine ( ";" ) ;
331
361
}
0 commit comments