Remove the dependency on System.ValueTuple (#63)
* Add an option to get tuple data via reflection. * Removed non-relfection-based way of getting information about the tuples. * Make methods static back. * Remove the nuget dependency to System.ValueTuple
This commit is contained in:
committed by
Ben Adams
parent
7381924470
commit
125e373b45
@@ -25,9 +25,6 @@
|
||||
<PackageReference Include="System.Reflection.Metadata">
|
||||
<Version>1.5.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.ValueTuple">
|
||||
<Version>4.4.0</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -508,7 +508,8 @@ namespace System.Diagnostics
|
||||
{
|
||||
return "out";
|
||||
}
|
||||
else if (parameterType != null && parameterType.IsByRef)
|
||||
|
||||
if (parameterType != null && parameterType.IsByRef)
|
||||
{
|
||||
var attribs = parameter.GetCustomAttributes(inherit: false);
|
||||
if (attribs?.Length > 0)
|
||||
@@ -549,15 +550,13 @@ namespace System.Diagnostics
|
||||
{
|
||||
var customAttribs = parameter.GetCustomAttributes(inherit: false);
|
||||
|
||||
if ((customAttribs?.Length ?? 0) > 0)
|
||||
{
|
||||
var tupleNames = customAttribs
|
||||
.OfType<TupleElementNamesAttribute>().FirstOrDefault()?.TransformNames;
|
||||
var tupleNameAttribute = customAttribs.OfType<Attribute>().FirstOrDefault(a => a.IsTupleElementNameAttribue());
|
||||
|
||||
if (tupleNames?.Count > 0)
|
||||
{
|
||||
return GetValueTupleParameter(tupleNames, prefix, parameter.Name, parameterType);
|
||||
}
|
||||
var tupleNames = tupleNameAttribute?.GetTransformerNames();
|
||||
|
||||
if (tupleNames?.Count > 0)
|
||||
{
|
||||
return GetValueTupleParameter(tupleNames, prefix, parameter.Name, parameterType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// Copyright (c) Ben A Adams. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Threading;
|
||||
|
||||
namespace System.Diagnostics.Internal
|
||||
{
|
||||
@@ -11,6 +12,8 @@ namespace System.Diagnostics.Internal
|
||||
/// </summary>
|
||||
public static class ReflectionHelper
|
||||
{
|
||||
private static PropertyInfo tranformerNamesLazyPropertyInfo;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if <paramref name="type"/> is <code>System.Runtime.CompilerServices.IsReadOnlyAttribute</code>.
|
||||
/// </summary>
|
||||
@@ -26,5 +29,40 @@ namespace System.Diagnostics.Internal
|
||||
{
|
||||
return type.Namespace == "System" && type.Name.Contains("ValueTuple`");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given <paramref name="attribute"/> is of type <code>TupleElementNameAttribute</code>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// To avoid compile-time depencency hell with System.ValueTuple, this method uses reflection and not checks statically that
|
||||
/// the given <paramref name="attribute"/> is <code>TupleElementNameAttribute</code>.
|
||||
/// </remarks>
|
||||
public static bool IsTupleElementNameAttribue(this Attribute attribute)
|
||||
{
|
||||
var attributeType = attribute.GetType();
|
||||
return attributeType.Namespace == "System.Runtime.CompilerServices" &&
|
||||
attributeType.Name == "TupleElementNamesAttribute";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns 'TransformNames' property value from a given <paramref name="attribute"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// To avoid compile-time depencency hell with System.ValueTuple, this method uses reflection
|
||||
/// instead of casting the attribute to a specific type.
|
||||
/// </remarks>
|
||||
public static IList<string> GetTransformerNames(this Attribute attribute)
|
||||
{
|
||||
Debug.Assert(attribute.IsTupleElementNameAttribue());
|
||||
|
||||
var propertyInfo = GetTransformNamesPropertyInfo(attribute.GetType());
|
||||
return (IList<string>)propertyInfo.GetValue(attribute);
|
||||
}
|
||||
|
||||
private static PropertyInfo GetTransformNamesPropertyInfo(Type attributeType)
|
||||
{
|
||||
return LazyInitializer.EnsureInitialized(ref tranformerNamesLazyPropertyInfo,
|
||||
() => attributeType.GetProperty("TransformNames", BindingFlags.Instance | BindingFlags.Public));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user