Skip to content

Commit 84ce89c

Browse files
committed
Use switch-case for property assignment, faster for types with more than 15 properties
1 parent 1155ef3 commit 84ce89c

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

MapDataReader/MapperGenerator.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,33 @@ public static void SetPropertyByName(this {typeNodeSymbol.FullName()} target, st
4747
4848
private static void SetPropertyByUpperName(this {typeNodeSymbol.FullName()} target, string name, object value)
4949
{{
50-
{"\r\n" + allProperties.Select(p =>
50+
switch (name)
51+
{{
52+
{allProperties.Select(p =>
5153
{
5254
var pTypeName = p.Type.FullName();
5355
5456
if (p.Type.IsReferenceType) //ref types - just cast to property type
5557
{
56-
return $@" if (name == ""{p.Name.ToUpperInvariant()}"") {{ target.{p.Name} = value as {pTypeName}; return; }}";
58+
return $@" case ""{p.Name.ToUpperInvariant()}"": target.{p.Name} = value as {pTypeName}; break;";
5759
}
5860
else if (pTypeName.EndsWith("?") && !p.Type.IsNullableEnum()) //nullable type (unless nullable Enum)
5961
{
6062
var nonNullableTypeName = pTypeName.TrimEnd('?');
6163
6264
//do not use "as" operator becasue "as" is slow for nullable types. Use "is" and a null-check
63-
return $@" if (name == ""{p.Name.ToUpperInvariant()}"") {{ if(value==null) target.{p.Name}=null; else if(value is {nonNullableTypeName}) target.{p.Name}=({nonNullableTypeName})value; return; }}";
65+
return $@" case ""{p.Name.ToUpperInvariant()}"": if(value==null) target.{p.Name}=null; else if(value is {nonNullableTypeName}) target.{p.Name}=({nonNullableTypeName})value; break;";
6466
}
6567
else if (p.Type.TypeKind == TypeKind.Enum || p.Type.IsNullableEnum()) //enum? pre-convert to underlying type then to int, you can't cast a boxed int to enum directly. Also to support assigning "smallint" database col to int32 (for example), which does not work at first (you can't cast a boxed "byte" to "int")
6668
{
67-
return $@" if (value != null && name == ""{p.Name.ToUpperInvariant()}"") {{ target.{p.Name} = ({pTypeName})(value.GetType() == typeof(int) ? (int)value : (int)Convert.ChangeType(value, typeof(int))); return; }}"; //pre-convert enums to int first (after unboxing, see below)
69+
return $@" case ""{p.Name.ToUpperInvariant()}"": if(value != null) target.{p.Name} = ({pTypeName})(value.GetType() == typeof(int) ? (int)value : (int)Convert.ChangeType(value, typeof(int))); break;";
6870
}
6971
else //primitive types. use Convert.ChangeType before casting. To support assigning "smallint" database col to int32 (for example), which does not work at first (you can't cast a boxed "byte" to "int")
7072
{
71-
return $@" if (value != null && name == ""{p.Name.ToUpperInvariant()}"") {{ target.{p.Name} = value.GetType() == typeof({pTypeName}) ? ({pTypeName})value : ({pTypeName})Convert.ChangeType(value, typeof({pTypeName})); return; }}";
73+
return $@" case ""{p.Name.ToUpperInvariant()}"": if(value != null) target.{p.Name} = value.GetType() == typeof({pTypeName}) ? ({pTypeName})value : ({pTypeName})Convert.ChangeType(value, typeof({pTypeName})); break;";
7274
}
73-
}).StringConcat("\r\n") }
74-
75-
75+
}).StringConcat("\r\n") }
76+
}}
7677
}} //end method";
7778

7879
if (typeNodeSymbol.InstanceConstructors.Any(c => !c.Parameters.Any())) //has a constructor without parameters?

0 commit comments

Comments
 (0)