<commit_before>﻿// Copyright (c) 2015 <NAME>
// See License.txt file for more details

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace ThinMvvm.Internals
{
    /// <summary>
    /// Delegate that does its best to only have a weak reference to its target.
    /// </summary>
    internal sealed class WeakDelegate
    {
        // Namespaces belonging to .NET or WinRT, their private methods most likely cannot be called.
        private static readonly string[] FrameworkNamespaces = { "System.", "Microsoft.", "Windows." };

        private readonly MethodInfo _method;
        // If the delegate isn't friendly to reflection, bail out and keep a strong reference.
        private readonly Delegate _original;
        // If the delegate is a closure, a strong reference to the target is needed, since there are no other references to it
        private readonly object _targetStrongRef;
        // Otherwise we have a normal delegate and we only keep a weak reference
        private readonly WeakReference<object> _targetRef;

        /// <summary>
        /// Creates a new instance of the <see cref="WeakDelegate" /> class with the specified delegate.
        /// </summary>
        /// <param name="wrapped">The delegate to be wrapped.</param>
        public WeakDelegate( Delegate wrapped )
        {
            _method = wrapped.GetMethodInfo();

            var targetType = wrapped.Target == null ? null : wrapped.Target.GetType().GetTypeInfo();
            _original =
                // Open delegate
                ( wrapped.Target == null && !_method.IsStatic )
                // Private method in a framework namespace, possibly untrusted, bail out
             || ( !_method.IsPublic && targetType != null && FrameworkNamespaces.Any( n => targetType.FullName.StartsWith( n ) ) )
                // Private type, possibly untrusted code, better bail out
             || ( targetType != null && targetType.IsNotPublic && ( !targetType.IsNested || targetType.IsNestedPublic ) ) ?
                wrapped : null;

            _targetStrongRef =
                _original == null && _method.DeclaringType.GetTypeInfo().GetCustomAttribute<CompilerGeneratedAttribute>() == null ?
                null : wrapped.Target;

            _targetRef =
                _targetStrongRef == null && wrapped.Target != null ?
                new WeakReference<object>( wrapped.Target ) : null;
        }<commit_msg>More tweaking of WeakDelegate.<commit_after>﻿// Copyright (c) 2015 <NAME>
// See License.txt file for more details

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace ThinMvvm.Internals
{
    /// <summary>
    /// Delegate that does its best to only have a weak reference to its target.
    /// </summary>
    internal sealed class WeakDelegate
    {
        // Namespaces belonging to .NET or WinRT, their private methods most likely cannot be called.
        private static readonly string[] FrameworkNamespaces = { "System.", "Microsoft.", "Windows." };

        private readonly MethodInfo _method;
        // If the delegate isn't friendly to reflection, bail out and keep a strong reference.
        private readonly Delegate _original;
        // If the delegate is a closure, a strong reference to the target is needed, since there are no other references to it
        private readonly object _targetStrongRef;
        // Otherwise we have a normal delegate and we only keep a weak reference
        private readonly WeakReference<object> _targetRef;

        /// <summary>
        /// Creates a new instance of the <see cref="WeakDelegate" /> class with the specified delegate.
        /// </summary>
        /// <param name="wrapped">The delegate to be wrapped.</param>
        public WeakDelegate( Delegate wrapped )
        {
            _method = wrapped.GetMethodInfo();

            var targetType = _method.DeclaringType.GetTypeInfo();
            _original =
                // Open delegate
                ( wrapped.Target == null && !_method.IsStatic )
                // Private method in a framework namespace, possibly untrusted, bail out
             || ( !_method.IsPublic && targetType != null && FrameworkNamespaces.Any( n => targetType.FullName.StartsWith( n ) ) )
                // Private type, possibly untrusted code, better bail out
             || ( targetType != null && targetType.IsNotPublic && ( !targetType.IsNested || targetType.IsNestedPublic ) ) ?
                wrapped : null;

            _targetStrongRef =
                _original == null && _method.DeclaringType.GetTypeInfo().GetCustomAttribute<CompilerGeneratedAttribute>() == null ?
                null : wrapped.Target;

            _targetRef =
                _targetStrongRef == null && wrapped.Target != null ?
                new WeakReference<object>( wrapped.Target ) : null;
        }
{'fraction_non_alphanumeric': 0.09269776876267749, 'fraction_numerical': 0.0016227180527383367, 'mean_word_length': 2.3205387205387207, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '953175', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1156, 'n_words': 432}