Skip to content

Commit f47b8d0

Browse files
committed
Releasing 5.9.6
2 parents 1a9b69c + 7f601e5 commit f47b8d0

18 files changed

+269
-167
lines changed

package.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project>
22

33
<PropertyGroup>
4-
<VersionBase>5.9.5</VersionBase>
4+
<VersionBase>5.9.6</VersionBase>
55
<PackageReleaseNotes>This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7</PackageReleaseNotes>
66
</PropertyGroup>
77

88
<PropertyGroup>
9-
<UnityAbstractionsVersion>4.0.*</UnityAbstractionsVersion>
9+
<UnityAbstractionsVersion>4.1.*</UnityAbstractionsVersion>
1010
<TargetFrameworks>netstandard2.0;netstandard1.0;netcoreapp2.0;netcoreapp1.0;net47;net46;net45;net40</TargetFrameworks>
1111
</PropertyGroup>
1212

src/Builder/Context/BuilderContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@ public object Resolve(Type type, string name)
4141
// Process overrides if any
4242
if (null != Overrides)
4343
{
44+
NamedType namedType = new NamedType
45+
{
46+
Type = type,
47+
Name = name
48+
};
49+
4450
// Check if this parameter is overridden
4551
for (var index = Overrides.Length - 1; index >= 0; --index)
4652
{
4753
var resolverOverride = Overrides[index];
48-
4954
// If matches with current parameter
5055
if (resolverOverride is IResolve resolverPolicy &&
51-
resolverOverride is IEquatable<(Type, string)> comparer && comparer.Equals((type, name)))
56+
resolverOverride is IEquatable<NamedType> comparer && comparer.Equals(namedType))
5257
{
5358
var context = this;
5459

src/Injection/Validating.cs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static class Validating
4545
if (method.IsInitialized) throw new InvalidOperationException("Sharing InjectionMethod between registrations is not supported");
4646

4747
// Select Method
48-
foreach (var info in method.DeclaredMembers(type))
48+
foreach (var info in type.GetDeclaredMethods())
4949
{
5050
if (!method.Data.MatchMemberInfo(info)) continue;
5151

@@ -68,26 +68,34 @@ public static class Validating
6868
if (selection.IsStatic)
6969
{
7070
throw new ArgumentException(
71-
$"The method {type?.Name}.{method.Name}({method.Data.Signature()}) is static. Static methods cannot be injected.");
71+
$"Static method {method.Name} on type '{selection.DeclaringType.Name}' cannot be injected");
7272
}
7373

74+
if (selection.IsPrivate)
75+
throw new InvalidOperationException(
76+
$"Private method '{method.Name}' on type '{selection.DeclaringType.Name}' cannot be injected");
77+
78+
if (selection.IsFamily)
79+
throw new InvalidOperationException(
80+
$"Protected method '{method.Name}' on type '{selection.DeclaringType.Name}' cannot be injected");
81+
7482
if (selection.IsGenericMethodDefinition)
7583
{
7684
throw new ArgumentException(
77-
$"The method {type?.Name}.{method.Name}({method.Data.Signature()}) is an open generic method. Open generic methods cannot be injected.");
85+
$"Open generic method {method.Name} on type '{selection.DeclaringType.Name}' cannot be injected");
7886
}
7987

8088
var parameters = selection.GetParameters();
8189
if (parameters.Any(param => param.IsOut))
8290
{
8391
throw new ArgumentException(
84-
$"The method {type?.Name}.{method.Name}({method.Data.Signature()}) has at least one out parameter. Methods with out parameters cannot be injected.");
92+
$"Method {method.Name} on type '{selection.DeclaringType.Name}' cannot be injected. Methods with 'out' parameters are not injectable.");
8593
}
8694

8795
if (parameters.Any(param => param.ParameterType.IsByRef))
8896
{
8997
throw new ArgumentException(
90-
$"The method {type?.Name}.{method.Name}({method.Data.Signature()}) has at least one ref parameter.Methods with ref parameters cannot be injected.");
98+
$"Method {method.Name} on type '{selection.DeclaringType.Name}' cannot be injected. Methods with 'ref' parameters are not injectable.");
9199
}
92100

93101
return selection;
@@ -100,10 +108,11 @@ public static class Validating
100108
FieldInfo selection = null;
101109
var field = (InjectionMember<FieldInfo, object>)member;
102110

103-
if (field.IsInitialized) throw new InvalidOperationException("Sharing InjectionField between registrations is not supported");
111+
if (field.IsInitialized) throw new InvalidOperationException(
112+
"Sharing InjectionField between registrations is not supported");
104113

105114
// Select Field
106-
foreach (var info in field.DeclaredMembers(type))
115+
foreach (var info in type.GetDeclaredFields())
107116
{
108117
if (info.Name != field.Name) continue;
109118

@@ -118,17 +127,25 @@ public static class Validating
118127
$"Injected field '{field.Name}' could not be matched with any public field on type '{type?.Name}'.");
119128
}
120129

130+
if (selection.IsStatic)
131+
throw new InvalidOperationException(
132+
$"Static field '{selection.Name}' on type '{type?.Name}' cannot be injected");
133+
121134
if (selection.IsInitOnly)
122-
{
123-
throw new ArgumentException(
124-
$"Field '{selection.Name}' on type '{type?.Name}' is Read Only and can not be injected.");
125-
}
135+
throw new InvalidOperationException(
136+
$"Readonly field '{selection.Name}' on type '{type?.Name}' cannot be injected");
137+
138+
if (selection.IsPrivate)
139+
throw new InvalidOperationException(
140+
$"Private field '{selection.Name}' on type '{type?.Name}' cannot be injected");
141+
142+
if (selection.IsFamily)
143+
throw new InvalidOperationException(
144+
$"Protected field '{selection.Name}' on type '{type?.Name}' cannot be injected");
126145

127146
if (!field.Data.Matches(selection.FieldType))
128-
{
129147
throw new ArgumentException(
130148
$"Injected data '{field.Data}' could not be matched with type of field '{selection.FieldType.Name}'.");
131-
}
132149

133150
return selection;
134151
};
@@ -144,7 +161,7 @@ public static class Validating
144161
if (property.IsInitialized) throw new InvalidOperationException("Sharing InjectionProperty between registrations is not supported");
145162

146163
// Select Property
147-
foreach (var info in property.DeclaredMembers(type))
164+
foreach (var info in type.GetDeclaredProperties())
148165
{
149166
if (info.Name != property.Name) continue;
150167

@@ -156,14 +173,30 @@ public static class Validating
156173
if (null == selection)
157174
{
158175
throw new ArgumentException(
159-
$"Injected property '{property.Name}' could not be matched with any public property on type '{type?.Name}'.");
176+
$"Injected property '{property.Name}' could not be matched with any property on type '{type?.Name}'.");
160177
}
161178

162179
if (!selection.CanWrite)
163-
{
164-
throw new ArgumentException(
165-
$"Property '{selection.Name}' on type '{type?.Name}' is Read Only and can not be injected.");
166-
}
180+
throw new InvalidOperationException(
181+
$"Readonly property '{selection.Name}' on type '{type?.Name}' cannot be injected");
182+
183+
if (0 != selection.GetIndexParameters().Length)
184+
throw new InvalidOperationException(
185+
$"Indexer '{selection.Name}' on type '{type?.Name}' cannot be injected");
186+
187+
var setter = selection.GetSetMethod(true);
188+
189+
if (setter.IsStatic)
190+
throw new InvalidOperationException(
191+
$"Static property '{selection.Name}' on type '{type?.Name}' cannot be injected");
192+
193+
if (setter.IsPrivate)
194+
throw new InvalidOperationException(
195+
$"Private property '{selection.Name}' on type '{type?.Name}' cannot be injected");
196+
197+
if (setter.IsFamily)
198+
throw new InvalidOperationException(
199+
$"Protected property '{selection.Name}' on type '{type?.Name}' cannot be injected");
167200

168201
if (!property.Data.Matches(selection.PropertyType))
169202
{
@@ -173,5 +206,6 @@ public static class Validating
173206

174207
return selection;
175208
};
209+
176210
}
177211
}

src/Processors/Abstracts/MemberProcessor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ public virtual IEnumerable<object> Select(Type type, IPolicySet registration)
216216

217217
protected abstract IEnumerable<TMemberInfo> DeclaredMembers(Type type);
218218

219-
protected virtual void ValidateMember(TMemberInfo info) { }
220-
221219
protected object PreProcessResolver(TMemberInfo info, object resolver)
222220
{
223221
switch (resolver)

src/Processors/Constructor/ConstructorProcessor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,9 @@ public override IEnumerable<object> Select(Type type, IPolicySet registration)
8181

8282
protected override IEnumerable<ConstructorInfo> DeclaredMembers(Type type)
8383
{
84-
#if NETSTANDARD1_0
8584
return type.GetTypeInfo()
8685
.DeclaredConstructors
87-
.Where(c => c.IsStatic == false && c.IsPublic);
88-
#else
89-
return type.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
90-
#endif
86+
.Where(ctor => !ctor.IsFamily && !ctor.IsPrivate && !ctor.IsStatic);
9187
}
9288

9389
#endregion

src/Processors/Fields/FieldDiagnostic.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq.Expressions;
34
using System.Reflection;
45
using Unity.Builder;
6+
using Unity.Injection;
57
using Unity.Policy;
8+
using Unity.Registration;
69
using Unity.Resolution;
710

811
namespace Unity.Processors
@@ -20,6 +23,54 @@ public FieldDiagnostic(IPolicySet policySet) : base(policySet)
2023

2124
#region Overrides
2225

26+
public override IEnumerable<object> Select(Type type, IPolicySet registration)
27+
{
28+
HashSet<object> memberSet = new HashSet<object>();
29+
30+
// Select Injected Members
31+
if (null != ((InternalRegistration)registration).InjectionMembers)
32+
{
33+
foreach (var injectionMember in ((InternalRegistration)registration).InjectionMembers)
34+
{
35+
if (injectionMember is InjectionMember<FieldInfo, object> && memberSet.Add(injectionMember))
36+
yield return injectionMember;
37+
}
38+
}
39+
40+
// Select Attributed members
41+
foreach (var member in type.GetDeclaredFields())
42+
{
43+
for (var i = 0; i < AttributeFactories.Length; i++)
44+
{
45+
#if NET40
46+
if (!member.IsDefined(AttributeFactories[i].Type, true) ||
47+
#else
48+
if (!member.IsDefined(AttributeFactories[i].Type) ||
49+
#endif
50+
!memberSet.Add(member)) continue;
51+
52+
if (member.IsStatic)
53+
throw new InvalidOperationException(
54+
$"Static field '{member.Name}' on type '{type?.Name}' is marked for injection. Static fields cannot be injected");
55+
56+
if (member.IsInitOnly)
57+
throw new InvalidOperationException(
58+
$"Readonly field '{member.Name}' on type '{type?.Name}' is marked for injection. Readonly fields cannot be injected");
59+
60+
if (member.IsPrivate)
61+
throw new InvalidOperationException(
62+
$"Private field '{member.Name}' on type '{type?.Name}' is marked for injection. Private fields cannot be injected");
63+
64+
if (member.IsFamily)
65+
throw new InvalidOperationException(
66+
$"Protected field '{member.Name}' on type '{type?.Name}' is marked for injection. Protected fields cannot be injected");
67+
68+
yield return member;
69+
break;
70+
}
71+
}
72+
}
73+
2374
protected override Expression GetResolverExpression(FieldInfo field, object resolver)
2475
{
2576
var ex = Expression.Variable(typeof(Exception));

src/Processors/Fields/FieldProcessor.cs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ public FieldProcessor(IPolicySet policySet)
2525

2626
protected override IEnumerable<FieldInfo> DeclaredMembers(Type type)
2727
{
28-
#if NETSTANDARD1_0
29-
return GetFieldsHierarchical(type).Where(f => !f.IsInitOnly && !f.IsStatic);
30-
#else
31-
return type.GetFields(BindingFlags.Instance | BindingFlags.Public)
32-
.Where(f => !f.IsInitOnly && !f.IsStatic);
33-
#endif
28+
return type.GetDeclaredFields()
29+
.Where(member => !member.IsFamily && !member.IsPrivate &&
30+
!member.IsInitOnly && !member.IsStatic);
3431
}
3532

3633
protected override Type MemberType(FieldInfo info) => info.FieldType;
@@ -68,28 +65,5 @@ protected override ResolveDelegate<BuilderContext> GetResolverDelegate(FieldInfo
6865
}
6966

7067
#endregion
71-
72-
73-
#region Implementation
74-
#if NETSTANDARD1_0
75-
76-
public static IEnumerable<FieldInfo> GetFieldsHierarchical(Type type)
77-
{
78-
if (type == null)
79-
{
80-
return Enumerable.Empty<FieldInfo>();
81-
}
82-
83-
if (type == typeof(object))
84-
{
85-
return type.GetTypeInfo().DeclaredFields;
86-
}
87-
88-
return type.GetTypeInfo()
89-
.DeclaredFields
90-
.Concat(GetFieldsHierarchical(type.GetTypeInfo().BaseType));
91-
}
92-
#endif
93-
#endregion
9468
}
9569
}

0 commit comments

Comments
 (0)