Skip to content

Commit d7231ee

Browse files
committed
Code cleanup
1 parent ac74df4 commit d7231ee

File tree

5 files changed

+97
-58
lines changed

5 files changed

+97
-58
lines changed

.editorconfig

+55-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Documentation:
2+
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
3+
14
# Top-most EditorConfig file
25
root = true
36

@@ -20,31 +23,68 @@ dotnet_style_qualification_for_property = false:warning
2023
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
2124
dotnet_style_predefined_type_for_member_access = true:warning
2225

26+
# Suggest explicit accessibility modifiers
27+
dotnet_style_require_accessibility_modifiers = always:suggestion
28+
2329
# Suggest more modern language features when available
30+
dotnet_style_explicit_tuple_names = true:warning
31+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
32+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
33+
dotnet_style_prefer_conditional_expression_over_assignment = true:none
34+
dotnet_style_prefer_conditional_expression_over_return = true:none
2435
dotnet_style_coalesce_expression = true:suggestion
2536
dotnet_style_null_propagation = true:suggestion
2637

27-
# Name all constant fields using PascalCase
28-
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
29-
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
30-
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
38+
# Definitions
39+
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum, delegate, type_parameter
40+
dotnet_naming_symbols.methods_properties.applicable_kinds = method, local_function, property
41+
dotnet_naming_symbols.public_symbols.applicable_kinds = property, method, field, event
42+
dotnet_naming_symbols.public_symbols.applicable_accessibilities = public
3143
dotnet_naming_symbols.constant_fields.applicable_kinds = field
3244
dotnet_naming_symbols.constant_fields.required_modifiers = const
45+
dotnet_naming_symbols.private_protected_internal_fields.applicable_kinds = field
46+
dotnet_naming_symbols.private_protected_internal_fields.applicable_accessibilities = private, protected, internal
47+
dotnet_naming_symbols.parameters_locals.applicable_kinds = parameter, local
3348
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
49+
dotnet_naming_style.camel_case_style.capitalization = camel_case
3450

35-
# Internal and private fields should be camelCase
36-
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
37-
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
38-
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
39-
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
40-
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
41-
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
51+
# Name all types using PascalCase
52+
dotnet_naming_rule.types_must_be_capitalized.symbols = types
53+
dotnet_naming_rule.types_must_be_capitalized.style = pascal_case_style
54+
dotnet_naming_rule.types_must_be_capitalized.severity = warning
55+
56+
# Name all methods and properties using PascalCase
57+
dotnet_naming_rule.methods_properties_must_be_capitalized.symbols = methods_properties
58+
dotnet_naming_rule.methods_properties_must_be_capitalized.style = pascal_case_style
59+
dotnet_naming_rule.methods_properties_must_be_capitalized.severity = warning
60+
61+
# Name all public members using PascalCase
62+
dotnet_naming_rule.public_members_must_be_capitalized.symbols = public_symbols
63+
dotnet_naming_rule.public_members_must_be_capitalized.style = pascal_case_style
64+
dotnet_naming_rule.public_members_must_be_capitalized.severity = warning
65+
66+
# Name all constant fields using PascalCase
67+
dotnet_naming_rule.constant_fields_must_be_pascal_case.symbols = constant_fields
68+
dotnet_naming_rule.constant_fields_must_be_pascal_case.style = pascal_case_style
69+
dotnet_naming_rule.constant_fields_must_be_pascal_case.severity = suggestion
70+
71+
# Name all private and internal fields using camelCase
72+
dotnet_naming_rule.private_protected_internal_fields_must_be_camel_case.symbols = private_protected_internal_fields
73+
dotnet_naming_rule.private_protected_internal_fields_must_be_camel_case.style = camel_case_style
74+
dotnet_naming_rule.private_protected_internal_fields_must_be_camel_case.severity = warning
75+
76+
# Name all parameters and locals using camelCase
77+
dotnet_naming_rule.parameters_locals_must_be_camel_case.symbols = parameters_locals
78+
dotnet_naming_rule.parameters_locals_must_be_camel_case.style = camel_case_style
79+
dotnet_naming_rule.parameters_locals_must_be_camel_case.severity = warning
4280

4381
[*.cs]
82+
csharp_preferred_modifier_order = public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async:suggestion
83+
4484
# Only use "var" when it's obvious what the variable type is
4585
csharp_style_var_for_built_in_types = false:warning
4686
csharp_style_var_when_type_is_apparent = true:suggestion
47-
#csharp_style_var_elsewhere = false:suggestion
87+
csharp_style_var_elsewhere = false:none
4888

4989
# Prefer method-like constructs to have a block body
5090
csharp_style_expression_bodied_methods = false:none
@@ -62,6 +102,7 @@ csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
62102
csharp_style_inlined_variable_declaration = true:suggestion
63103
csharp_style_throw_expression = true:suggestion
64104
csharp_style_conditional_delegate_call = true:suggestion
105+
csharp_style_pattern_local_over_anonymous_function = true:suggestion
65106

66107
# Newline settings
67108
csharp_new_line_before_open_brace = all
@@ -76,7 +117,7 @@ csharp_indent_block_contents = true
76117
csharp_indent_braces = false
77118
csharp_indent_case_contents = true
78119
csharp_indent_switch_labels = true
79-
csharp_indent_labels = one_less
120+
csharp_indent_labels = one_less_than_current
80121

81122
# Space preferences
82123
csharp_space_after_cast = false
@@ -99,13 +140,9 @@ csharp_space_between_method_call_parameter_list_parentheses = false
99140
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
100141
csharp_space_between_method_declaration_name_and_open_parenthesis = false
101142
csharp_space_between_method_declaration_parameter_list_parentheses = false
102-
csharp_space_between_parentheses = none
143+
csharp_space_between_parentheses = false
103144
csharp_space_between_square_brackets = false
104145

105-
# TODO: see https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
106-
# dotnet_prefer_inferred_tuple_names
107-
# dotnet_prefer_inferred_anonymous_type_member_names
108-
109146
[*.{xml,csproj,targets,props,json}]
110147
indent_size = 2
111148
indent_style = space

DeepConvert.Tests/DeepConvertTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,10 @@ public class DataClass
385385

386386
public class DataClass2
387387
{
388+
#pragma warning disable IDE1006 // Lower case member name is intentional for testing purposes
388389
public int flag { get; set; }
389390
public long number { get; set; }
391+
#pragma warning restore IDE1006
390392
public string Text { get; set; }
391393
public double[] Floats { get; set; }
392394
public bool Add { get; set; } = true;

DeepConvert/ConvertTypeInfo.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ internal class ConvertTypeInfo
1414
{
1515
#region Static members
1616

17-
private static AssemblyBuilder assemblyBuilder;
18-
private static ModuleBuilder moduleBuilder;
17+
private static readonly AssemblyBuilder assemblyBuilder;
18+
private static readonly ModuleBuilder moduleBuilder;
1919

2020
static ConvertTypeInfo()
2121
{
@@ -247,7 +247,7 @@ private void BuildSetMethod(TypeBuilder typeBuilder, List<PropertyInfo> properti
247247
ilGen.Emit(OpCodes.Ldloc_0);
248248
ilGen.Emit(OpCodes.Ldarg_2);
249249
ilGen.Emit(OpCodes.Ldtoken, propertyInfo.PropertyType);
250-
ilGen.EmitCall(OpCodes.Call, MethodOf(() => Type.GetTypeFromHandle(default(RuntimeTypeHandle))), null);
250+
ilGen.EmitCall(OpCodes.Call, MethodOf(() => Type.GetTypeFromHandle(default)), null);
251251
ilGen.Emit(OpCodes.Ldarg_3);
252252
ilGen.EmitCall(OpCodes.Call, MethodOf(() => DeepConvert.ChangeType(null, null, null)), null);
253253
if (propertyInfo.PropertyType.IsValueType)

DeepConvert/DeepConvert.cs

+36-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2019, Yves Goergen, https://unclassified.software
1+
// Copyright (c) 2018-2020, Yves Goergen, https://unclassified.software
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
44
// associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -63,7 +63,7 @@ public static partial class DeepConvert
6363
/// <summary>
6464
/// The UNIX epoch time.
6565
/// </summary>
66-
private static DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
66+
private static readonly DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
6767

6868
#endregion Private data
6969

@@ -139,7 +139,7 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
139139
if (Nullable.GetUnderlyingType(destType) != null)
140140
{
141141
// Special handling for empty strings
142-
if (value is string && (string)value == "")
142+
if (value is string str && str == "")
143143
return null;
144144

145145
return ChangeType(value, Nullable.GetUnderlyingType(destType), settings);
@@ -188,103 +188,103 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
188188
{
189189
if (destType == typeof(bool)) return !falseWords.Any(w => string.Equals(w, (string)value, StringComparison.OrdinalIgnoreCase));
190190
if (destType == typeof(BigInteger)) return BigInteger.Parse((string)value, provider);
191-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //DateTime.Parse((string)value, provider);
191+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
192192
if (destType == typeof(TimeSpan)) return TimeSpan.Parse((string)value, provider);
193193
if (destType == typeof(Guid)) return Guid.Parse((string)value);
194194
}
195195
if (srcType == typeof(char))
196196
{
197-
if (destType == typeof(bool)) return !value.Equals((char)0) ? true : false;
197+
if (destType == typeof(bool)) return !value.Equals((char)0);
198198
if (destType == typeof(BigInteger)) return new BigInteger((char)value);
199-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((char)value);
199+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
200200
if (destType == typeof(TimeSpan)) return new TimeSpan((char)value);
201201
// No conversion to Guid
202202
}
203203
if (srcType == typeof(byte))
204204
{
205-
if (destType == typeof(bool)) return !value.Equals((byte)0) ? true : false;
205+
if (destType == typeof(bool)) return !value.Equals((byte)0);
206206
if (destType == typeof(BigInteger)) return new BigInteger((byte)value);
207-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((byte)value);
207+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
208208
if (destType == typeof(TimeSpan)) return new TimeSpan((byte)value);
209209
// No conversion to Guid
210210
}
211211
if (srcType == typeof(sbyte))
212212
{
213-
if (destType == typeof(bool)) return !value.Equals((sbyte)0) ? true : false;
213+
if (destType == typeof(bool)) return !value.Equals((sbyte)0);
214214
if (destType == typeof(BigInteger)) return new BigInteger((sbyte)value);
215-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((sbyte)value);
215+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
216216
if (destType == typeof(TimeSpan)) return new TimeSpan((sbyte)value);
217217
// No conversion to Guid
218218
}
219219
if (srcType == typeof(short))
220220
{
221-
if (destType == typeof(bool)) return !value.Equals((short)0) ? true : false;
221+
if (destType == typeof(bool)) return !value.Equals((short)0);
222222
if (destType == typeof(BigInteger)) return new BigInteger((short)value);
223-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((short)value);
223+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
224224
if (destType == typeof(TimeSpan)) return new TimeSpan((short)value);
225225
// No conversion to Guid
226226
}
227227
if (srcType == typeof(ushort))
228228
{
229-
if (destType == typeof(bool)) return !value.Equals((ushort)0) ? true : false;
229+
if (destType == typeof(bool)) return !value.Equals((ushort)0);
230230
if (destType == typeof(BigInteger)) return new BigInteger((ushort)value);
231-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((ushort)value);
231+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
232232
if (destType == typeof(TimeSpan)) return new TimeSpan((ushort)value);
233233
// No conversion to Guid
234234
}
235235
if (srcType == typeof(int))
236236
{
237-
if (destType == typeof(bool)) return !value.Equals(0) ? true : false;
237+
if (destType == typeof(bool)) return !value.Equals(0);
238238
if (destType == typeof(BigInteger)) return new BigInteger((int)value);
239-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((int)value);
239+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
240240
if (destType == typeof(TimeSpan)) return new TimeSpan((int)value);
241241
// No conversion to Guid
242242
}
243243
if (srcType == typeof(uint))
244244
{
245-
if (destType == typeof(bool)) return !value.Equals((uint)0) ? true : false;
245+
if (destType == typeof(bool)) return !value.Equals((uint)0);
246246
if (destType == typeof(BigInteger)) return new BigInteger((uint)value);
247-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((uint)value);
247+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
248248
if (destType == typeof(TimeSpan)) return new TimeSpan((uint)value);
249249
// No conversion to Guid
250250
}
251251
if (srcType == typeof(long))
252252
{
253-
if (destType == typeof(bool)) return !value.Equals((long)0) ? true : false;
253+
if (destType == typeof(bool)) return !value.Equals((long)0);
254254
if (destType == typeof(BigInteger)) return new BigInteger((long)value);
255-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((long)value);
255+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
256256
if (destType == typeof(TimeSpan)) return new TimeSpan((long)value);
257257
// No conversion to Guid
258258
}
259259
if (srcType == typeof(ulong))
260260
{
261-
if (destType == typeof(bool)) return !value.Equals((ulong)0) ? true : false;
261+
if (destType == typeof(bool)) return !value.Equals((ulong)0);
262262
if (destType == typeof(BigInteger)) return new BigInteger((ulong)value);
263-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((long)(ulong)value);
263+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
264264
if (destType == typeof(TimeSpan)) return new TimeSpan((long)(ulong)value);
265265
// No conversion to Guid
266266
}
267267
if (srcType == typeof(float))
268268
{
269-
if (destType == typeof(bool)) return !value.Equals((float)0) ? true : false;
269+
if (destType == typeof(bool)) return !value.Equals((float)0);
270270
if (destType == typeof(BigInteger)) return new BigInteger((float)value);
271-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((long)(float)value);
271+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
272272
if (destType == typeof(TimeSpan)) return new TimeSpan((long)(float)value);
273273
// No conversion to Guid
274274
}
275275
if (srcType == typeof(double))
276276
{
277-
if (destType == typeof(bool)) return !value.Equals((double)0) ? true : false;
277+
if (destType == typeof(bool)) return !value.Equals((double)0);
278278
if (destType == typeof(BigInteger)) return new BigInteger((double)value);
279-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((long)(double)value);
279+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
280280
if (destType == typeof(TimeSpan)) return new TimeSpan((long)(double)value);
281281
// No conversion to Guid
282282
}
283283
if (srcType == typeof(decimal))
284284
{
285-
if (destType == typeof(bool)) return !value.Equals((decimal)0) ? true : false;
285+
if (destType == typeof(bool)) return !value.Equals((decimal)0);
286286
if (destType == typeof(BigInteger)) return new BigInteger((decimal)value);
287-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime((long)(decimal)value);
287+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
288288
if (destType == typeof(TimeSpan)) return new TimeSpan((long)(decimal)value);
289289
// No conversion to Guid
290290
}
@@ -306,9 +306,9 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
306306
object enumNumericValue = Convert.ChangeType(enumValue, Enum.GetUnderlyingType(destType), provider);
307307
return Enum.ToObject(destType, enumNumericValue);
308308
}
309-
if (destType == typeof(bool)) return !enumValue.Equals(0) ? true : false;
309+
if (destType == typeof(bool)) return !enumValue.Equals(0);
310310
if (destType == typeof(BigInteger)) return new BigInteger(Convert.ToInt64(enumValue));
311-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime(Convert.ToInt64(enumValue));
311+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
312312
if (destType == typeof(TimeSpan)) return new TimeSpan(Convert.ToInt64(enumValue));
313313
// No conversion to Guid
314314
}
@@ -354,9 +354,9 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
354354
object enumNumericValue = Convert.ChangeType(((BigInteger)value).ToString(), Enum.GetUnderlyingType(destType), provider);
355355
return Enum.ToObject(destType, enumNumericValue);
356356
}
357-
if (destType == typeof(bool)) return !((BigInteger)value).IsZero ? true : false;
357+
if (destType == typeof(bool)) return !((BigInteger)value).IsZero;
358358
if (destType == typeof(BigInteger)) return value;
359-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime(Convert.ToInt64(((BigInteger)value).ToString(), provider));
359+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
360360
if (destType == typeof(TimeSpan)) return new TimeSpan(Convert.ToInt64(((BigInteger)value).ToString(), provider));
361361
// No conversion to Guid
362362
}
@@ -392,9 +392,9 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
392392
throw new ArgumentException("Unknown date numeric kind.");
393393
}
394394
}
395-
if (destType == typeof(bool)) return ((DateTime)value).Ticks != 0 ? true : false;
395+
if (destType == typeof(bool)) return ((DateTime)value).Ticks != 0;
396396
if (destType == typeof(DateTime)) return value;
397-
if (destType == typeof(TimeSpan)) return ((DateTime)value).TimeOfDay; //new TimeSpan(((DateTime)value).Ticks);
397+
if (destType == typeof(TimeSpan)) return ((DateTime)value).TimeOfDay;
398398
// No conversion to Guid
399399
}
400400
if (srcType == typeof(TimeSpan))
@@ -430,8 +430,8 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
430430
}
431431
//return Convert.ChangeType(((TimeSpan)value).Ticks, destType);
432432
}
433-
if (destType == typeof(bool)) return ((TimeSpan)value).Ticks != 0 ? true : false;
434-
if (destType == typeof(DateTime)) return ToDateTime(value, settings); //new DateTime(((TimeSpan)value).Ticks);
433+
if (destType == typeof(bool)) return ((TimeSpan)value).Ticks != 0;
434+
if (destType == typeof(DateTime)) return ToDateTime(value, settings);
435435
if (destType == typeof(TimeSpan)) return value;
436436
// No conversion to Guid
437437
}

DeepConvert/DeepConvertSettings.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ public enum DateNumericKind
9191
/// <summary>
9292
/// Milliseconds since UNIX epoch, 1970-01-01T00:00:00Z (JavaScript).
9393
/// </summary>
94-
UnixMilliseconds,
94+
UnixMilliseconds
9595
}
9696
}

0 commit comments

Comments
 (0)