@@ -329,7 +329,7 @@ public void GenerateCode(SourceProductionContext context)
329329 ReturnStatement ( BinaryExpression ( SyntaxKind . AddExpression , BinaryExpression (
330330 SyntaxKind . AddExpression , InvocationExpression ( MemberAccessExpression (
331331 SyntaxKind . SimpleMemberAccessExpression ,
332- IdentifierName ( "Value" ) ,
332+ IdentifierName ( UnitString is { } argText ? $ "As( { argText } )" : "Value" ) ,
333333 IdentifierName ( "ToString" ) ) )
334334 . WithArgumentList ( ArgumentList (
335335 [
@@ -950,6 +950,23 @@ MemberDeclarationSyntax CreateEnumerableMethod(string methodName, string xml)
950950 }
951951 }
952952
953+ private string ? UnitString
954+ {
955+ get
956+ {
957+ if ( quantitySymbol ? . GetAttributes ( ) . FirstOrDefault ( a =>
958+ a . AttributeClass is { IsGenericType : true } attributeClass
959+ && attributeClass . ConstructUnboundGenericType ( ) . GetName ( ) . FullName is
960+ "global::TedToolkit.Quantities.QuantityDisplayUnitAttribute<>" ) is not
961+ { } displayUnitAttribute ) return null ;
962+
963+ var syntax = displayUnitAttribute . ApplicationSyntaxReference ? . GetSyntax ( )
964+ as AttributeSyntax ;
965+ var argSyntax = syntax ? . ArgumentList ? . Arguments [ 0 ] . Expression ;
966+ return argSyntax ? . ToString ( ) ;
967+ }
968+ }
969+
953970 private ExpressionSyntax GetSystemUnitName ( )
954971 {
955972 var allUnits = quantity . Units
@@ -963,32 +980,25 @@ private ExpressionSyntax GetSystemUnitName()
963980 Literal ( quantity . Name ) ) ;
964981 }
965982
966- if ( quantitySymbol ? . GetAttributes ( ) . FirstOrDefault ( a =>
967- a . AttributeClass is { IsGenericType : true } attributeClass
968- && attributeClass . ConstructUnboundGenericType ( ) . GetName ( ) . FullName is
969- "global::TedToolkit.Quantities.QuantityDisplayUnitAttribute<>" ) is { } displayUnitAttribute )
983+ if ( UnitString is { } argText )
970984 {
971- var syntax = displayUnitAttribute . ApplicationSyntaxReference ? . GetSyntax ( )
972- as AttributeSyntax ;
973- var argSyntax = syntax ? . ArgumentList ? . Arguments [ 0 ] . Expression ;
974- var argText = argSyntax ? . ToString ( ) ;
975- if ( argText is not null )
976- return InvocationExpression (
977- MemberAccessExpression (
978- SyntaxKind . SimpleMemberAccessExpression ,
979- IdentifierName ( argText ) ,
980- IdentifierName ( "ToString" ) ) )
981- . WithArgumentList ( ArgumentList (
982- [
983- Argument ( IdentifierName ( "index" ) ) ,
984- Argument ( IdentifierName ( "formatProvider" ) )
985- ] ) ) ;
985+ return InvocationExpression (
986+ MemberAccessExpression (
987+ SyntaxKind . SimpleMemberAccessExpression ,
988+ IdentifierName ( argText ) ,
989+ IdentifierName ( "ToString" ) ) )
990+ . WithArgumentList ( ArgumentList (
991+ [
992+ Argument ( IdentifierName ( "index" ) ) ,
993+ Argument ( IdentifierName ( "formatProvider" ) )
994+ ] ) ) ;
986995 }
987996
988997 if ( quantity . IsNoDimensions )
989998 {
990999 var memberName = allUnits
9911000 . OrderBy ( u => u . DistanceToDefault )
1001+ . ThenByDescending ( u => u . ApplicableSystem )
9921002 . First ( ) . GetUnitName ( data . Units . Values ) ;
9931003 return FromMember ( quantity . UnitName , memberName ) ;
9941004 }
@@ -1010,6 +1020,7 @@ private ExpressionSyntax GetSystemUnitName()
10101020 return true ;
10111021 } )
10121022 . OrderBy ( u => u . DistanceToDefault )
1023+ . ThenByDescending ( u => u . ApplicableSystem )
10131024 . FirstOrDefault ( ) ;
10141025
10151026 if ( ! string . IsNullOrEmpty ( choiceUnit . Name ) )
0 commit comments