Skip to content

Commit

Permalink
Extend the class::static handling to also support public static fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaud DESODT committed Nov 21, 2017
1 parent a20dce5 commit e81439a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/Serilog/Settings/KeyValuePairs/SettingValueConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@ public static object ConvertToType(string value, Type toType)
.Where(x => x.GetMethod.IsPublic)
.FirstOrDefault(x => x.GetMethod.IsStatic);

if (publicStaticPropertyInfo == null)
if (publicStaticPropertyInfo != null)
{
throw new InvalidOperationException($"Could not find public static property `{memberName}` on type `{accessorTypeName}`");
return publicStaticPropertyInfo.GetValue(null); // static property, no instance to pass
}
return publicStaticPropertyInfo.GetValue(null); // static property, no instance to pass

// no property ? look for a field
var publicStaticFieldInfo = accessorType.GetTypeInfo().DeclaredFields
.Where(x => x.Name == memberName)
.Where(x => x.IsPublic)
.FirstOrDefault(x => x.IsStatic);

if (publicStaticFieldInfo != null)
{
return publicStaticFieldInfo.GetValue(null); // static field, no instance to pass
}

throw new InvalidOperationException($"Could not find a public static property or field with name `{memberName}` on type `{accessorTypeName}`");
}

var toTypeInfo = toType.GetTypeInfo();
Expand Down
2 changes: 1 addition & 1 deletion test/Serilog.Tests/Settings/KeyValuePairSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public void LoggingLevelSwitchCanBeUsedForMinimumLevelOverrides()
var systemLogger = log.ForContext(Constants.SourceContextPropertyName, "System.Bar");

log.Write(Some.InformationEvent());
Assert.False(evt is null, "Minimul level is Debug. It should log Information messages");
Assert.False(evt is null, "Minimum level is Debug. It should log Information messages");

evt = null;
// ReSharper disable HeuristicUnreachableCode
Expand Down
12 changes: 9 additions & 3 deletions test/Serilog.Tests/Settings/SettingValueConversionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public void TryParseStaticMemberAccessorReturnsExpectedResults(string input, str
[Theory]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::StringProperty, Serilog.Tests", typeof(string), "StringProperty")]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::IntProperty, Serilog.Tests", typeof(int), 42)]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::StringField, Serilog.Tests", typeof(string), "StringField")]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::IntField, Serilog.Tests", typeof(int), 666)]
public void StaticMembersAccessorsCanBeUsedForSImpleTypes(string input, Type targetType, object expectedValue)
{
var actual = SettingValueConversions.ConvertToType(input, targetType);
Expand All @@ -136,6 +138,8 @@ public void StaticMembersAccessorsCanBeUsedForSImpleTypes(string input, Type tar
[Theory]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::InterfaceProperty, Serilog.Tests", typeof(IAmAnInterface))]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::AbstractProperty, Serilog.Tests", typeof(AnAbstractClass))]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::InterfaceField, Serilog.Tests", typeof(IAmAnInterface))]
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::AbstractField, Serilog.Tests", typeof(AnAbstractClass))]
public void StaticMembersAccessorsCanBeUsedForReferenceTypes(string input, Type targetType)
{
var actual = SettingValueConversions.ConvertToType(input, targetType);
Expand Down Expand Up @@ -164,17 +168,19 @@ public void StaticAccessorOnUnknownTypeThrowsTypeLoadException(string input, Typ
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::UnknownMember, Serilog.Tests", typeof(string))]
// static property exists but it's private
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::PrivateStringProperty, Serilog.Tests", typeof(string))]
// public member exists but it's a field
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::StringField, Serilog.Tests", typeof(string))]
// static field exists but it's private
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::PrivateStringField, Serilog.Tests", typeof(string))]
// public property exists but it's not static
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::InstanceStringProperty, Serilog.Tests", typeof(string))]
// public field exists but it's not static
[InlineData("Serilog.Tests.Support.ClassWithStaticAccessors::InstanceStringField, Serilog.Tests", typeof(string))]
public void StaticAccessorWithInvalidMemberThrowsInvalidOperationException(string input, Type targetType)
{
var exception = Assert.Throws<InvalidOperationException>(() =>
SettingValueConversions.ConvertToType(input, targetType)
);

Assert.Contains("Could not find public static property ", exception.Message);
Assert.Contains("Could not find a public static property or field ", exception.Message);
Assert.Contains("on type `Serilog.Tests.Support.ClassWithStaticAccessors, Serilog.Tests`", exception.Message);
}
}
Expand Down
12 changes: 11 additions & 1 deletion test/Serilog.Tests/Support/StaticAccessorClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ public class ClassWithStaticAccessors
public static int IntProperty => 42;
public static IAmAnInterface InterfaceProperty => ConcreteImpl.Instance;
public static AnAbstractClass AbstractProperty => ConcreteImpl.Instance;

public static string StringField = nameof(StringField);
public static int IntField = 666;
public static IAmAnInterface InterfaceField = ConcreteImpl.Instance;
public static AnAbstractClass AbstractField = ConcreteImpl.Instance;

// ReSharper disable once UnusedMember.Local
static string PrivateStringProperty => nameof(PrivateStringProperty);
#pragma warning disable 169
static string PrivateStringField = nameof(PrivateStringField);
#pragma warning restore 169
public string InstanceStringProperty => nameof(InstanceStringProperty);
public static string StringField = nameof(StringField);
public string InstanceStringField = nameof(InstanceStringField);

}
}

0 comments on commit e81439a

Please sign in to comment.