-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
XamlResourceIdAttribute.cs
86 lines (79 loc) · 2.91 KB
/
XamlResourceIdAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#nullable disable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Microsoft.Maui.Controls.Xaml
{
/// <include file="../../../docs/Microsoft.Maui.Controls.Xaml/XamlResourceIdAttribute.xml" path="Type[@FullName='Microsoft.Maui.Controls.Xaml.XamlResourceIdAttribute']/Docs/*" />
[AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
public sealed class XamlResourceIdAttribute : Attribute
{
/// <include file="../../../docs/Microsoft.Maui.Controls.Xaml/XamlResourceIdAttribute.xml" path="//Member[@MemberName='ResourceId']/Docs/*" />
public string ResourceId { get; set; }
/// <include file="../../../docs/Microsoft.Maui.Controls.Xaml/XamlResourceIdAttribute.xml" path="//Member[@MemberName='Path']/Docs/*" />
public string Path { get; set; }
/// <include file="../../../docs/Microsoft.Maui.Controls.Xaml/XamlResourceIdAttribute.xml" path="//Member[@MemberName='Type']/Docs/*" />
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type Type { get; set; }
/// <include file="../../../docs/Microsoft.Maui.Controls.Xaml/XamlResourceIdAttribute.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public XamlResourceIdAttribute(
string resourceId,
string path,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
{
ResourceId = resourceId;
Path = path;
Type = type;
}
internal static string GetResourceIdForType(Type type)
{
var assembly = type.Assembly;
foreach (var xria in assembly.GetCustomAttributes<XamlResourceIdAttribute>())
{
if (xria.Type == type)
return xria.ResourceId;
}
return null;
}
internal static string GetPathForType(Type type)
{
var assembly = type.Assembly;
foreach (var xria in assembly.GetCustomAttributes<XamlResourceIdAttribute>())
{
if (xria.Type == type)
return xria.Path;
}
return null;
}
internal static string GetResourceIdForPath(Assembly assembly, string path)
{
foreach (var xria in assembly.GetCustomAttributes<XamlResourceIdAttribute>())
{
if (xria.Path == path)
return xria.ResourceId;
}
return null;
}
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
internal static Type GetTypeForResourceId(Assembly assembly, string resourceId)
{
foreach (var xria in assembly.GetCustomAttributes<XamlResourceIdAttribute>())
{
if (xria.ResourceId == resourceId)
return xria.Type;
}
return null;
}
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
internal static Type GetTypeForPath(Assembly assembly, string path)
{
foreach (var xria in assembly.GetCustomAttributes<XamlResourceIdAttribute>())
{
if (xria.Path == path)
return xria.Type;
}
return null;
}
}
}