You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if(p.Type.IsReferenceType)//ref types - just cast to property type
55
57
{
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;";
57
59
}
58
60
elseif(pTypeName.EndsWith("?")&&!p.Type.IsNullableEnum())//nullable type (unless nullable Enum)
59
61
{
60
62
varnonNullableTypeName=pTypeName.TrimEnd('?');
61
63
62
64
//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;";
64
66
}
65
67
elseif(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")
66
68
{
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)
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")
0 commit comments