Skip to content

Commit 422a10f

Browse files
committed
Fix possible exception in SetBackingField for empty properties.
1 parent a8f4b2c commit 422a10f

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

AssemblyToProcess/WithInterceptors.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,57 @@ private void SetInterceptor(object value, string propertyName)
3131
public string Property2 { get; set; }
3232
}
3333

34+
public class ClassWithReadonlyProperty
35+
{
36+
private int _field = 42;
37+
38+
[GetInterceptor]
39+
private object GetInterceptor(string propertyName, Type propertyType)
40+
{
41+
return Convert.ChangeType(_field, propertyType);
42+
}
43+
44+
[SetInterceptor]
45+
private void SetInterceptor(object value, string propertyName)
46+
{
47+
_field = Convert.ToInt32(value);
48+
}
49+
50+
public int Property1 { get; set; }
51+
52+
public string Property2 { get; set; }
53+
54+
public string Property3 { get; }
55+
}
56+
57+
public abstract class ClassWithAbstractProperty
58+
{
59+
private int _field = 42;
60+
61+
[GetInterceptor]
62+
protected object GetInterceptor(string propertyName, Type propertyType)
63+
{
64+
return Convert.ChangeType(_field, propertyType);
65+
}
66+
67+
[SetInterceptor]
68+
protected void SetInterceptor(object value, string propertyName)
69+
{
70+
_field = Convert.ToInt32(value);
71+
}
72+
73+
public abstract int Property1 { get; set; }
74+
75+
public abstract string Property2 { get; set; }
76+
}
77+
78+
public class ClassDerivedFromClassWithAbstractProperty : ClassWithAbstractProperty
79+
{
80+
public override int Property1 { get; set; }
81+
82+
public override string Property2 { get; set; }
83+
}
84+
3485
public class ClassWithGenericInterceptors
3586
{
3687
private int _field = 42;

AutoProperties.Fody/InstructionSequences.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public InstructionSequences([NotNull, ItemNotNull] IList<Instruction> instructio
2222
[NotNull, ItemNotNull]
2323
private static IEnumerable<InstructionSequence> CreateSequences([NotNull, ItemNotNull] IList<Instruction> instructions, [CanBeNull, ItemNotNull] IList<SequencePoint> sequencePoints)
2424
{
25-
if (sequencePoints == null)
25+
if (sequencePoints?.Any() != true)
2626
{
2727
yield return new InstructionSequence(instructions, null, instructions.Count, null);
2828
yield break;

Tests/InterceptorTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class InterceptorTests
1818
[TestCase("ClassWithExternalInterceptorsBase", 0)]
1919
[TestCase("ClassWithExternalGenericInterceptorsBase", 0)]
2020
[TestCase("ClassWithInterceptorsUsingAllPossibleParameters", 3)]
21+
[TestCase("ClassWithReadonlyProperty", 1)]
22+
[TestCase("ClassDerivedFromClassWithAbstractProperty", 0)]
2123
public void SimpleInterceptorTest([NotNull] string className, int expectedNumberOfFields)
2224
{
2325
var target = assembly.GetInstance(className);

0 commit comments

Comments
 (0)