Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetCore Native reflection works better when attributes are allocated #2979

Merged
merged 1 commit into from Oct 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/NLog/Internal/PropertyHelper.cs
Expand Up @@ -55,6 +55,15 @@ internal static class PropertyHelper
{
private static Dictionary<Type, Dictionary<string, PropertyInfo>> parameterInfoCache = new Dictionary<Type, Dictionary<string, PropertyInfo>>();

#pragma warning disable S1144 // Unused private types or members should be removed. BUT they help CoreRT to provide config through reflection
private static readonly RequiredParameterAttribute _requiredParameterAttribute = new RequiredParameterAttribute();
private static readonly ArrayParameterAttribute _arrayParameterAttribute = new ArrayParameterAttribute(null, string.Empty);
private static readonly DefaultValueAttribute _defaultValueAttribute = new DefaultValueAttribute(string.Empty);
private static readonly AdvancedAttribute _advancedAttribute = new AdvancedAttribute();
private static readonly DefaultParameterAttribute _defaultParameterAttribute = new DefaultParameterAttribute();
private static readonly FlagsAttribute _flagsAttribute = new FlagsAttribute();
#pragma warning restore S1144 // Unused private types or members should be removed

/// <summary>
/// Set value parsed from string.
/// </summary>
Expand All @@ -75,7 +84,7 @@ internal static void SetPropertyFromString(object obj, string propertyName, stri

try
{
if (propInfo.IsDefined(typeof(ArrayParameterAttribute), false))
if (propInfo.IsDefined(_arrayParameterAttribute.GetType(), false))
{
throw new NotSupportedException($"Parameter {propertyName} of {obj.GetType().Name} is an array and cannot be assigned a scalar value.");
}
Expand Down Expand Up @@ -128,7 +137,7 @@ internal static bool IsArrayProperty(Type t, string propertyName)
throw new NotSupportedException($"Parameter {propertyName} not supported on {t.Name}");
}

return propInfo.IsDefined(typeof(ArrayParameterAttribute), false);
return propInfo.IsDefined(_arrayParameterAttribute.GetType(), false);
}

/// <summary>
Expand Down Expand Up @@ -177,7 +186,7 @@ internal static void CheckRequiredParameters(object o)
{
foreach (PropertyInfo propInfo in GetAllReadableProperties(o.GetType()))
{
if (propInfo.IsDefined(typeof(RequiredParameterAttribute), false))
if (propInfo.IsDefined(_requiredParameterAttribute.GetType(), false))
{
object value = propInfo.GetValue(o, null);
if (value == null)
Expand Down Expand Up @@ -247,7 +256,7 @@ private static bool TryGetEnumValue(Type resultType, string value, out object re
return false;
}

if (flagsEnumAllowed && resultType.IsDefined(typeof(FlagsAttribute), false))
if (flagsEnumAllowed && resultType.IsDefined(_flagsAttribute.GetType(), false))
{
ulong union = 0;

Expand Down Expand Up @@ -454,7 +463,7 @@ private static bool TryGetPropertyInfo(Type targetType, string propertyName, out
retVal[propInfo.Name] = propInfo;
}

if (propInfo.IsDefined(typeof(DefaultParameterAttribute), false))
if (propInfo.IsDefined(_defaultParameterAttribute.GetType(), false))
{
// define a property with empty name
retVal[string.Empty] = propInfo;
Expand Down