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

WIP: add trimming support #917

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net70</TargetFramework>
<TargetFramework>net80</TargetFramework>
<PublishAot>true</PublishAot>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<Nullable>enable</Nullable>
Expand Down
5 changes: 0 additions & 5 deletions YamlDotNet/Core/CharacterAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public bool IsAlphaNumericDashOrUnderscore(int offset = 0)
character == '-';
}

public bool IsAscii(int offset = 0)
{
return Buffer.Peek(offset) <= '\x7F';
}

public bool IsPrintable(int offset = 0)
{
var character = Buffer.Peek(offset);
Expand Down
38 changes: 0 additions & 38 deletions YamlDotNet/Helpers/ReadOnlyCollectionExtensions.cs

This file was deleted.

134 changes: 42 additions & 92 deletions YamlDotNet/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;

Expand All @@ -43,11 +44,6 @@ public static bool IsGenericType(this Type type)
return type.GetTypeInfo().IsGenericType;
}

public static bool IsGenericTypeDefinition(this Type type)
{
return type.GetTypeInfo().IsGenericTypeDefinition;
}

public static bool IsInterface(this Type type)
{
return type.GetTypeInfo().IsInterface;
Expand All @@ -66,7 +62,11 @@ public static bool IsEnum(this Type type)
/// <returns>
/// <c>true</c> if the type has a default constructor; otherwise, <c>false</c>.
/// </returns>
public static bool HasDefaultConstructor(this Type type, bool allowPrivateConstructors)
public static bool HasDefaultConstructor(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
#endif
this Type type, bool allowPrivateConstructors)
{
var bindingFlags = BindingFlags.Instance | BindingFlags.Public;

Expand All @@ -78,16 +78,6 @@ public static bool HasDefaultConstructor(this Type type, bool allowPrivateConstr
return type.IsValueType || type.GetConstructor(bindingFlags, null, Type.EmptyTypes, null) != null;
}

public static bool IsAssignableFrom(this Type type, Type source)
{
return type.IsAssignableFrom(source.GetTypeInfo());
}

public static bool IsAssignableFrom(this Type type, TypeInfo source)
{
return type.GetTypeInfo().IsAssignableFrom(source);
}

public static TypeCode GetTypeCode(this Type type)
{
var isEnum = type.IsEnum();
Expand Down Expand Up @@ -164,60 +154,65 @@ public static TypeCode GetTypeCode(this Type type)

public static bool IsDbNull(this object value)
{
return value?.GetType()?.FullName == "System.DBNull";
}

public static Type[] GetGenericArguments(this Type type)
{
return type.GetTypeInfo().GenericTypeArguments;
return value.GetType().FullName == "System.DBNull";
}

public static PropertyInfo? GetPublicProperty(this Type type, string name)
{
return type.GetRuntimeProperty(name);
}

public static FieldInfo? GetPublicStaticField(this Type type, string name)
{
return type.GetRuntimeField(name);
}


private static readonly Func<PropertyInfo, bool> IsInstance = (PropertyInfo property) => !(property.GetMethod ?? property.SetMethod).IsStatic;
private static readonly Func<PropertyInfo, bool> IsInstancePublic = (PropertyInfo property) => IsInstance(property) && (property.GetMethod ?? property.SetMethod).IsPublic;

public static IEnumerable<PropertyInfo> GetProperties(this Type type, bool includeNonPublic)
public static IEnumerable<PropertyInfo> GetProperties(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces |
DynamicallyAccessedMemberTypes.PublicProperties |
DynamicallyAccessedMemberTypes.NonPublicProperties)]
#endif
this Type type, bool includeNonPublic)
{
var predicate = includeNonPublic ? IsInstance : IsInstancePublic;

return type.IsInterface()
? (new Type[] { type })
.Concat(type.GetInterfaces())
.SelectMany(i => i.GetRuntimeProperties().Where(predicate))
.SelectMany((
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
#endif
i) => i.GetRuntimeProperties().Where(predicate))
: type.GetRuntimeProperties().Where(predicate);
}

public static IEnumerable<PropertyInfo> GetPublicProperties(this Type type) => GetProperties(type, false);
public static IEnumerable<PropertyInfo> GetPublicProperties(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces |
DynamicallyAccessedMemberTypes.PublicProperties |
DynamicallyAccessedMemberTypes.NonPublicProperties)]
#endif
this Type type) => GetProperties(type, false);

public static IEnumerable<FieldInfo> GetPublicFields(this Type type)
public static IEnumerable<FieldInfo> GetPublicFields(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields |DynamicallyAccessedMemberTypes.NonPublicFields)]
#endif
this Type type)
{
return type.GetRuntimeFields().Where(f => !f.IsStatic && f.IsPublic);
}

public static IEnumerable<MethodInfo> GetPublicStaticMethods(this Type type)
public static IEnumerable<MethodInfo> GetPublicStaticMethods(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
this Type type)
{
return type.GetRuntimeMethods()
.Where(m => m.IsPublic && m.IsStatic);
}

public static MethodInfo GetPrivateStaticMethod(this Type type, string name)
{
return type.GetRuntimeMethods()
.FirstOrDefault(m => !m.IsPublic && m.IsStatic && m.Name.Equals(name))
?? throw new MissingMethodException($"Expected to find a method named '{name}' in '{type.FullName}'.");
}

public static MethodInfo? GetPublicStaticMethod(this Type type, string name, params Type[] parameterTypes)
public static MethodInfo? GetPublicStaticMethod(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
this Type type, string name, params Type[] parameterTypes)
{
return type.GetRuntimeMethods()
.FirstOrDefault(m =>
Expand All @@ -232,54 +227,9 @@ public static MethodInfo GetPrivateStaticMethod(this Type type, string name)
});
}

public static MethodInfo? GetPublicInstanceMethod(this Type type, string name)
{
return type.GetRuntimeMethods()
.FirstOrDefault(m => m.IsPublic && !m.IsStatic && m.Name.Equals(name));
}

public static MethodInfo? GetGetMethod(this PropertyInfo property, bool nonPublic)
{
var getter = property.GetMethod;
if (!nonPublic && !getter.IsPublic)
{
getter = null;
}
return getter;
}

public static MethodInfo? GetSetMethod(this PropertyInfo property)
{
return property.SetMethod;
}

public static IEnumerable<Type> GetInterfaces(this Type type)
{
return type.GetTypeInfo().ImplementedInterfaces;
}

public static bool IsInstanceOf(this Type type, object o)
{
return o.GetType() == type || o.GetType().GetTypeInfo().IsSubclassOf(type);
}

public static Attribute[] GetAllCustomAttributes<TAttribute>(this PropertyInfo member)
{
// IMemberInfo.GetCustomAttributes ignores it's "inherit" parameter for properties,
// and the suggested replacement (Attribute.GetCustomAttributes) is not available
// on netstandard1.3
var result = new List<Attribute>();
var type = member.DeclaringType;

while (type != null)
{
type.GetPublicProperty(member.Name);
result.AddRange(member.GetCustomAttributes(typeof(TAttribute)));

type = type.BaseType();
}

return result.ToArray();
return Attribute.GetCustomAttributes(member, typeof(TAttribute), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using System;
using System.Collections;
using YamlDotNet.Core;
using YamlDotNet.Serialization.ObjectFactories;

namespace YamlDotNet.Serialization.NodeDeserializers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Helpers;
Expand Down Expand Up @@ -79,6 +80,9 @@ public bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, o
return true;
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("<TODO>")]
#endif
internal static void DeserializeHelper(Type tItem, IParser parser, Func<IParser, Type, object?> nestedObjectDeserializer, IList result, bool canUpdate, INamingConvention enumNamingConvention)
{
parser.Consume<SequenceStart>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using System.Collections;
using System.Collections.Generic;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Helpers;
using YamlDotNet.Serialization.Utilities;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using YamlDotNet.Serialization.Callbacks;
Expand Down Expand Up @@ -143,7 +144,11 @@ private void ExecuteState(Type attributeType, object value)
}
}

private MethodInfo[] GetStateMethods(Type attributeType, Type valueType)
private MethodInfo[] GetStateMethods(Type attributeType,
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type valueType)
{
var stateDictionary = _stateMethods[attributeType];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using YamlDotNet.Core;

namespace YamlDotNet.Serialization.ObjectGraphVisitors
Expand All @@ -33,7 +34,11 @@ public DefaultExclusiveObjectGraphVisitor(IObjectGraphVisitor<IEmitter> nextVisi
{
}

private static object? GetDefault(Type type)
private static object? GetDefault(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
#endif
Type type)
{
return type.IsValueType() ? Activator.CreateInstance(type) : null;
}
Expand Down
Loading