Skip to content

Commit f59ad10

Browse files
committed
Refactoring to improve cognitive complexity.
1 parent 92474a9 commit f59ad10

6 files changed

Lines changed: 615 additions & 451 deletions

File tree

src/Autofac/Core/Activators/Reflection/AutowiringPropertyInjector.cs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,43 +113,59 @@ private static IEnumerable<PropertyInfo> GetInjectableProperties(Type instanceTy
113113
{
114114
foreach (var property in instanceType.GetRuntimeProperties())
115115
{
116-
if (!property.CanWrite)
116+
if (!IsInjectableProperty(property))
117117
{
118118
continue;
119119
}
120120

121-
// SetMethod will be non-null if CanWrite is true.
122-
// Don't want to inject onto static properties.
123-
if (property.SetMethod!.IsStatic)
124-
{
125-
continue;
126-
}
121+
yield return property;
122+
}
123+
}
127124

128-
var propertyType = property.PropertyType;
125+
private static bool IsInjectableProperty(PropertyInfo property)
126+
{
127+
// We only inject into assignable instance properties.
128+
if (!property.CanWrite)
129+
{
130+
return false;
131+
}
129132

130-
if (propertyType.IsValueType && !propertyType.IsEnum)
131-
{
132-
continue;
133-
}
133+
// SetMethod will be non-null if CanWrite is true.
134+
// Don't want to inject onto static properties.
135+
if (property.SetMethod!.IsStatic)
136+
{
137+
return false;
138+
}
134139

135-
// GetElementType will be non-null if IsArray is true.
136-
if (propertyType.IsArray && propertyType.GetElementType()!.IsValueType)
137-
{
138-
continue;
139-
}
140+
// Avoid attempting resolution for value-type shapes that Autofac does not
141+
// meaningfully construct via property injection.
142+
var propertyType = property.PropertyType;
143+
if (IsUnsupportedPropertyType(propertyType))
144+
{
145+
return false;
146+
}
140147

141-
if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GenericTypeArguments[0].IsValueType)
142-
{
143-
continue;
144-
}
148+
// Indexers require index arguments and are not regular injectable properties.
149+
return property.GetIndexParameters().Length == 0;
150+
}
145151

146-
if (property.GetIndexParameters().Length != 0)
147-
{
148-
continue;
149-
}
152+
private static bool IsUnsupportedPropertyType(Type propertyType)
153+
{
154+
// Primitive/value-type properties are not autowired (enums are allowed).
155+
if (propertyType.IsValueType && !propertyType.IsEnum)
156+
{
157+
return true;
158+
}
150159

151-
yield return property;
160+
// Arrays of value types behave like value containers; skip autowiring.
161+
// GetElementType will be non-null if IsArray is true.
162+
if (propertyType.IsArray && propertyType.GetElementType()!.IsValueType)
163+
{
164+
return true;
152165
}
166+
167+
// Also skip IEnumerable<TValueType> - same rule as arrays above.
168+
return propertyType.IsGenericEnumerableInterfaceType() && propertyType.GenericTypeArguments[0].IsValueType;
153169
}
154170

155171
[SuppressMessage("S125", "S125", Justification = "Commented code explains the code generation output.")]

0 commit comments

Comments
 (0)