Skip to content

Commit 4cdb991

Browse files
Cleaned up code in the type invokeconstructor to initialize properties
1 parent 4897de7 commit 4cdb991

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/DotNetToolkit.Repository/Extensions/Internal/TypeExtensions.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ select pi
220220
{
221221
// Try to get all the values for the parameters we already have,
222222
// and set the rest to their default value
223-
var args = new List<object>();
224-
225-
args.AddRange(matchedCtorParams.Value.Select(pi =>
223+
var args = matchedCtorParams.Value.Select(pi =>
226224
{
227225
// If we find a matching parameter, then delete it from the collection,
228226
// that way we don't try to initialize a property that has the same name
@@ -235,9 +233,9 @@ select pi
235233
}
236234

237235
return pi.ParameterType.GetDefault();
238-
}));
236+
}).ToArray();
239237

240-
obj = matchedCtorParams.Key.Invoke(args.ToArray());
238+
obj = matchedCtorParams.Key.Invoke(args);
241239
}
242240
else
243241
{
@@ -247,18 +245,25 @@ select pi
247245
if (kvs.Any())
248246
{
249247
// Try to initialize properties that match
250-
type.GetRuntimeProperties()
248+
var query = type
249+
.GetRuntimeProperties()
251250
.Where(x => x.CanWrite && x.GetSetMethod(nonPublic: true).IsPublic)
252251
.Join(kvs,
253252
pi => pi.Name,
254253
kv => kv.Key,
255254
(pi, kv) => new
256255
{
257256
PropertyInfo = pi,
258-
Value = pi.PropertyType.ConvertTo(kv.Value)
259-
})
260-
.ToList()
261-
.ForEach(q => q.PropertyInfo.SetValue(obj, q.Value));
257+
kv.Value
258+
});
259+
260+
foreach (var q in query)
261+
{
262+
var pi = q.PropertyInfo;
263+
var value = pi.PropertyType.ConvertTo(q.Value);
264+
265+
pi.SetValue(obj, value);
266+
}
262267
}
263268

264269
return obj;

0 commit comments

Comments
 (0)