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

[Trimming] Fix several trimming warnings #19402

Merged
16 changes: 9 additions & 7 deletions src/Controls/src/Core/BindableProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.Maui.Controls
public sealed class BindableProperty
{
internal const DynamicallyAccessedMemberTypes DeclaringTypeMembers = DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods;
internal const DynamicallyAccessedMemberTypes ReturnTypeMembers = DynamicallyAccessedMemberTypes.PublicParameterlessConstructor;

public delegate void BindingPropertyChangedDelegate(BindableObject bindable, object oldValue, object newValue);

Expand Down Expand Up @@ -70,7 +71,7 @@ public sealed class BindableProperty
/// <include file="../../docs/Microsoft.Maui.Controls/BindableProperty.xml" path="//Member[@MemberName='UnsetValue']/Docs/*" />
public static readonly object UnsetValue = new object();

BindableProperty(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,
BindableProperty(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,
ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
CoerceValueDelegate coerceValue = null, BindablePropertyBindingChanging bindingChanging = null, bool isReadOnly = false, CreateDefaultValueDelegate defaultValueCreator = null)
{
Expand Down Expand Up @@ -125,6 +126,7 @@ public sealed class BindableProperty
public string PropertyName { get; }

/// <include file="../../docs/Microsoft.Maui.Controls/BindableProperty.xml" path="//Member[@MemberName='ReturnType']/Docs/*" />
[DynamicallyAccessedMembers(ReturnTypeMembers)]
public Type ReturnType { get; }

internal BindablePropertyBindingChanging BindingChanging { get; private set; }
Expand All @@ -140,7 +142,7 @@ public sealed class BindableProperty
internal ValidateValueDelegate ValidateValue { get; private set; }

/// <include file="../../docs/Microsoft.Maui.Controls/BindableProperty.xml" path="//Member[@MemberName='Create']/Docs/*" />
public static BindableProperty Create(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue = null, BindingMode defaultBindingMode = BindingMode.OneWay,
public static BindableProperty Create(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue = null, BindingMode defaultBindingMode = BindingMode.OneWay,
ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
CoerceValueDelegate coerceValue = null, CreateDefaultValueDelegate defaultValueCreator = null)
{
Expand All @@ -149,15 +151,15 @@ public static BindableProperty Create(string propertyName, Type returnType, [Dyn
}

/// <include file="../../docs/Microsoft.Maui.Controls/BindableProperty.xml" path="//Member[@MemberName='CreateAttached']/Docs/*" />
public static BindableProperty CreateAttached(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,
public static BindableProperty CreateAttached(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,
ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
CoerceValueDelegate coerceValue = null, CreateDefaultValueDelegate defaultValueCreator = null)
{
return CreateAttached(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, null, false, defaultValueCreator);
}

/// <include file="../../docs/Microsoft.Maui.Controls/BindableProperty.xml" path="//Member[@MemberName='CreateAttachedReadOnly']/Docs/*" />
public static BindablePropertyKey CreateAttachedReadOnly(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWayToSource,
public static BindablePropertyKey CreateAttachedReadOnly(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWayToSource,
ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
CoerceValueDelegate coerceValue = null, CreateDefaultValueDelegate defaultValueCreator = null)
{
Expand All @@ -167,7 +169,7 @@ public static BindablePropertyKey CreateAttachedReadOnly(string propertyName, Ty
}

/// <include file="../../docs/Microsoft.Maui.Controls/BindableProperty.xml" path="//Member[@MemberName='CreateReadOnly']/Docs/*" />
public static BindablePropertyKey CreateReadOnly(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWayToSource,
public static BindablePropertyKey CreateReadOnly(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWayToSource,
ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
CoerceValueDelegate coerceValue = null, CreateDefaultValueDelegate defaultValueCreator = null)
{
Expand All @@ -176,15 +178,15 @@ public static BindablePropertyKey CreateReadOnly(string propertyName, Type retur
isReadOnly: true, defaultValueCreator: defaultValueCreator));
}

internal static BindableProperty Create(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate validateValue,
internal static BindableProperty Create(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate validateValue,
BindingPropertyChangedDelegate propertyChanged, BindingPropertyChangingDelegate propertyChanging, CoerceValueDelegate coerceValue, BindablePropertyBindingChanging bindingChanging,
CreateDefaultValueDelegate defaultValueCreator = null)
{
return new BindableProperty(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, bindingChanging,
defaultValueCreator: defaultValueCreator);
}

internal static BindableProperty CreateAttached(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate validateValue,
internal static BindableProperty CreateAttached(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate validateValue,
BindingPropertyChangedDelegate propertyChanged, BindingPropertyChangingDelegate propertyChanging, CoerceValueDelegate coerceValue, BindablePropertyBindingChanging bindingChanging,
bool isReadOnly, CreateDefaultValueDelegate defaultValueCreator = null)
{
Expand Down
40 changes: 28 additions & 12 deletions src/Controls/src/Core/DependencyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class DependencyService
static readonly object s_dependencyLock = new object();
static readonly object s_initializeLock = new object();

static readonly List<Type> DependencyTypes = new List<Type>();
static readonly List<DependencyType> DependencyTypes = new List<DependencyType>();
static readonly Dictionary<Type, DependencyData> DependencyImplementations = new Dictionary<Type, DependencyData>();

public static T Resolve<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(DependencyFetchTarget fallbackFetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
Expand Down Expand Up @@ -64,16 +64,14 @@ public static class DependencyService
public static void Register<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>() where T : class
{
Type type = typeof(T);
if (!DependencyTypes.Contains(type))
DependencyTypes.Add(type);
AddDependencyTypeIfNeeded(type);
}

public static void Register<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TImpl>() where T : class where TImpl : class, T
{
Type targetType = typeof(T);
Type implementorType = typeof(TImpl);
if (!DependencyTypes.Contains(targetType))
DependencyTypes.Add(targetType);
AddDependencyTypeIfNeeded(targetType);

lock (s_dependencyLock)
DependencyImplementations[targetType] = new DependencyData { ImplementorType = implementorType };
Expand All @@ -83,16 +81,22 @@ public static class DependencyService
{
Type targetType = typeof(T);
Type implementorType = typeof(T);
if (!DependencyTypes.Contains(targetType))
DependencyTypes.Add(targetType);
AddDependencyTypeIfNeeded(targetType);

lock (s_dependencyLock)
DependencyImplementations[targetType] = new DependencyData { ImplementorType = implementorType, GlobalInstance = instance };
}

static void AddDependencyTypeIfNeeded(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
{
if (!DependencyTypes.Any(t => t.Type == type))
DependencyTypes.Add(new DependencyType(type));
}

[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
static Type FindImplementor(Type target) =>
DependencyTypes.FirstOrDefault(t => target.IsAssignableFrom(t));
DependencyTypes.FirstOrDefault(t => target.IsAssignableFrom(t.Type))?.Type;
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved

// Once we get essentials/cg converted to using startup.cs
// we will delete the initialize code from here and just use
Expand Down Expand Up @@ -137,10 +141,7 @@ public static void Register(Assembly[] assemblies)
for (int i = 0; i < attributes.Length; i++)
{
DependencyAttribute attribute = (DependencyAttribute)attributes[i];
if (!DependencyTypes.Contains(attribute.Implementor))
{
DependencyTypes.Add(attribute.Implementor);
}
AddDependencyTypeIfNeeded(attribute.Implementor);
}
}
}
Expand Down Expand Up @@ -169,5 +170,20 @@ class DependencyData
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type ImplementorType { get; set; }
}

class DependencyType : IEquatable<DependencyType>
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type Type { get; }

public DependencyType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
{
Type = type;
}

public override int GetHashCode() => Type.GetHashCode();
public override bool Equals(object obj) => obj is DependencyType other && Equals(other);
public bool Equals(DependencyType other) => other.Type == Type;
}
}
}
12 changes: 7 additions & 5 deletions src/Controls/src/Core/Internals/CellExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ namespace Microsoft.Maui.Controls.Internals
[EditorBrowsable(EditorBrowsableState.Never)]
public static class CellExtensions
{
public static bool GetIsGroupHeader<TView, [DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
const DynamicallyAccessedMemberTypes ItemTypeMembers = BindableProperty.DeclaringTypeMembers | BindableProperty.ReturnTypeMembers;

public static bool GetIsGroupHeader<TView, [DynamicallyAccessedMembers(ItemTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
{
return TemplatedItemsList<TView, TItem>.GetIsGroupHeader(cell);
}

public static void SetIsGroupHeader<TView, [DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers)] TItem>(this TItem cell, bool value) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
public static void SetIsGroupHeader<TView, [DynamicallyAccessedMembers(ItemTypeMembers)] TItem>(this TItem cell, bool value) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
{
TemplatedItemsList<TView, TItem>.SetIsGroupHeader(cell, value);
}

public static TItem GetGroupHeaderContent<TView, [DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
public static TItem GetGroupHeaderContent<TView, [DynamicallyAccessedMembers(ItemTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
{
var group = TemplatedItemsList<TView, TItem>.GetGroup(cell);
return group.HeaderContent;
}

public static int GetIndex<TView, [DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
public static int GetIndex<TView, [DynamicallyAccessedMembers(ItemTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
{
return TemplatedItemsList<TView, TItem>.GetIndex(cell);
}

public static ITemplatedItemsList<TItem> GetGroup<TView, [DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
public static ITemplatedItemsList<TItem> GetGroup<TView, [DynamicallyAccessedMembers(ItemTypeMembers)] TItem>(this TItem cell) where TView : BindableObject, ITemplatedItemsView<TItem> where TItem : BindableObject
{
return TemplatedItemsList<TView, TItem>.GetGroup(cell);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/ItemsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/ItemsView.xml" path="Type[@FullName='Microsoft.Maui.Controls.ItemsView']/Docs/*" />
public abstract class ItemsView<[DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers)] TVisual> : View, ITemplatedItemsView<TVisual> where TVisual : BindableObject
public abstract class ItemsView<[DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers | BindableProperty.ReturnTypeMembers)] TVisual> : View, ITemplatedItemsView<TVisual> where TVisual : BindableObject
{
/*
/// <summary>Bindable property for <see cref="InfiniteScrolling"/>.</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/ResourceDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void SetAndLoadSource(Uri value, string resourcePath, Assembly assembly,
//this will return a type if the RD as an x:Class element, and codebehind
var type = XamlResourceIdAttribute.GetTypeForPath(assembly, resourcePath);
if (type != null)
_mergedInstance = s_instances.GetValue(type, (key) => (ResourceDictionary)Activator.CreateInstance(key));
_mergedInstance = s_instances.GetValue(type, _ => (ResourceDictionary)Activator.CreateInstance(type));
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved
else
_mergedInstance = DependencyService.Get<IResourcesLoader>().CreateFromResource<ResourceDictionary>(resourcePath, assembly, lineInfo);
OnValuesChanged(_mergedInstance.ToArray());
Expand Down
11 changes: 9 additions & 2 deletions src/Controls/src/Core/Routing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,15 @@ internal static void Clear()
}

/// <summary>Bindable property for attached property <c>Route</c>.</summary>
public static readonly BindableProperty RouteProperty =
BindableProperty.CreateAttached("Route", typeof(string), typeof(Routing), null,
public static readonly BindableProperty RouteProperty = CreateRouteProperty();

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2111:ReflectionToDynamicallyAccessedMembers",
Justification = "The CreateAttached method has a DynamicallyAccessedMembers annotation for all public methods"
+ "on the declaring type. This includes the Routing.RegisterRoute(string, Type) method which also has a "
+ "DynamicallyAccessedMembers annotation and the trimmer can't guarantee the availability of the requirements"
+ "of the method. The RegisterRoute method is never used via reflection so we can safely ignore the warning.")]
simonrozsival marked this conversation as resolved.
Show resolved Hide resolved
private static BindableProperty CreateRouteProperty()
=> BindableProperty.CreateAttached("Route", typeof(string), typeof(Routing), null,
defaultValueCreator: CreateDefaultRoute);

static object CreateDefaultRoute(BindableObject bindable)
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/TemplatedItemsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Maui.Controls.Internals
{

[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class TemplatedItemsList<TView, [DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers)] TItem> : BindableObject, ITemplatedItemsList<TItem>, IList, IDisposable
public sealed class TemplatedItemsList<TView, [DynamicallyAccessedMembers(BindableProperty.DeclaringTypeMembers | BindableProperty.ReturnTypeMembers)] TItem> : BindableObject, ITemplatedItemsList<TItem>, IList, IDisposable
where TView : BindableObject, IItemsView<TItem>
where TItem : BindableObject
{
Expand Down
Loading
Loading