forked from pakrym/jab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnownTypes.cs
More file actions
149 lines (121 loc) · 9.46 KB
/
Copy pathKnownTypes.cs
File metadata and controls
149 lines (121 loc) · 9.46 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace Jab;
internal class KnownTypes
{
public const string JabAttributesAssemblyName = "Jab.Attributes";
public const string TransientAttributeShortName = "Transient";
public const string SingletonAttributeShortName = "Singleton";
public const string ScopedAttributeShortName = "Scoped";
public const string CompositionRootAttributeShortName = "ServiceProvider";
public const string ServiceProviderModuleAttributeShortName = "ServiceProviderModule";
public const string ImportAttributeShortName = "Import";
public const string FromNamedServicesAttributeShortName = "FromNamedServices";
public const string ResolveDelegateShortName = "Resolve";
public const string NamedResolveDelegateShortName = "NamedResolve";
public const string TransientAttributeTypeName = $"{TransientAttributeShortName}Attribute";
public const string SingletonAttributeTypeName = $"{SingletonAttributeShortName}Attribute";
public const string ScopedAttributeTypeName = $"{ScopedAttributeShortName}Attribute";
public const string CompositionRootAttributeTypeName = $"{CompositionRootAttributeShortName}Attribute";
public const string ServiceProviderModuleAttributeTypeName = $"{ServiceProviderModuleAttributeShortName}Attribute";
public const string ImportAttributeTypeName = $"{ImportAttributeShortName}Attribute";
public const string FromNamedServicesAttributeName = $"{FromNamedServicesAttributeShortName}Attribute";
public const string ResolveDelegateTypeName = ResolveDelegateShortName;
public const string NamedResolveDelegateTypeName = NamedResolveDelegateShortName;
public const string TransientAttributeMetadataName = $"Jab.{TransientAttributeTypeName}";
public const string GenericTransientAttributeMetadataName = $"Jab.{TransientAttributeTypeName}`1";
public const string Generic2TransientAttributeMetadataName = $"Jab.{TransientAttributeTypeName}`2";
public const string SingletonAttributeMetadataName = $"Jab.{SingletonAttributeTypeName}";
public const string GenericSingletonAttributeMetadataName = $"Jab.{SingletonAttributeTypeName}`1";
public const string Generic2SingletonAttributeMetadataName = $"Jab.{SingletonAttributeTypeName}`2";
public const string ScopedAttributeMetadataName = $"Jab.{ScopedAttributeTypeName}";
public const string GenericScopedAttributeMetadataName = $"Jab.{ScopedAttributeTypeName}`1";
public const string Generic2ScopedAttributeMetadataName = $"Jab.{ScopedAttributeTypeName}`2";
public const string CompositionRootAttributeMetadataName = $"Jab.{CompositionRootAttributeTypeName}";
public const string ServiceProviderModuleAttributeMetadataName = $"Jab.{ServiceProviderModuleAttributeTypeName}";
public const string ImportAttributeMetadataName = $"Jab.{ImportAttributeTypeName}";
public const string GenericImportAttributeMetadataName = $"Jab.{ImportAttributeTypeName}`1";
public const string NameAttributePropertyName = "Name";
public const string InstanceAttributePropertyName = "Instance";
public const string FactoryAttributePropertyName = "Factory";
public const string RootServicesAttributePropertyName = "RootServices";
private const string IAsyncDisposableMetadataName = "System.IAsyncDisposable";
private const string IEnumerableMetadataName = "System.Collections.Generic.IEnumerable`1";
private const string IServiceProviderMetadataName = "System.IServiceProvider";
private const string IServiceScopeMetadataName = "Microsoft.Extensions.DependencyInjection.IServiceScope";
private const string IKeyedServiceProviderMetadataName = "Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider";
private const string FromKeyedServicesAttributeMetadataName = "Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute";
private const string FromNamedServicesAttributeMetadataName = $"Jab.{FromNamedServicesAttributeName}";
private const string ResolveDelegateMetadataName = $"Jab.{ResolveDelegateTypeName}`1";
private const string NamedResolveDelegateMetadataName = $"Jab.{NamedResolveDelegateTypeName}`1";
private const string IServiceScopeFactoryMetadataName =
"Microsoft.Extensions.DependencyInjection.IServiceScopeFactory";
private const string IServiceProviderIsServiceMetadataName =
"Microsoft.Extensions.DependencyInjection.IServiceProviderIsService";
public INamedTypeSymbol IEnumerableType { get; }
public INamedTypeSymbol IServiceProviderType { get; }
public INamedTypeSymbol CompositionRootAttributeType { get; }
public INamedTypeSymbol TransientAttributeType { get; }
public INamedTypeSymbol? GenericTransientAttributeType { get; }
public INamedTypeSymbol? Generic2TransientAttributeType { get; }
public INamedTypeSymbol SingletonAttribute { get; }
public INamedTypeSymbol? GenericSingletonAttribute { get; }
public INamedTypeSymbol? Generic2SingletonAttribute { get; }
public INamedTypeSymbol ImportAttribute { get; }
public INamedTypeSymbol? GenericImportAttribute { get; }
public INamedTypeSymbol ModuleAttribute { get; }
public INamedTypeSymbol ScopedAttribute { get; }
public INamedTypeSymbol? GenericScopedAttribute { get; }
public INamedTypeSymbol? Generic2ScopedAttribute { get; }
public INamedTypeSymbol? IAsyncDisposableType { get; }
public INamedTypeSymbol? IServiceScopeType { get; }
public INamedTypeSymbol? IServiceScopeFactoryType { get; }
public INamedTypeSymbol? IServiceProviderIsServiceType { get; }
public INamedTypeSymbol? IKeyedServiceProviderType { get; }
public INamedTypeSymbol? FromKeyedServicesAttribute { get; }
public INamedTypeSymbol? FromNamedServicesAttribute { get; }
public INamedTypeSymbol ResolveDelegateType { get; }
public INamedTypeSymbol NamedResolveDelegateType { get; }
public KnownTypes(Compilation compilation, IModuleSymbol module, IAssemblySymbol assemblySymbol)
{
assemblySymbol =
module.ReferencedAssemblySymbols.FirstOrDefault(
s => s.Name == JabAttributesAssemblyName)
?? assemblySymbol;
static INamedTypeSymbol GetTypeByMetadataNameOrThrow(IAssemblySymbol assemblySymbol,
string fullyQualifiedMetadataName) =>
assemblySymbol.GetTypeByMetadataName(fullyQualifiedMetadataName)
?? throw new InvalidOperationException($"Type with metadata '{fullyQualifiedMetadataName}' not found");
static INamedTypeSymbol GetTypeFromCompilationByMetadataNameOrThrow(Compilation compilation,
string fullyQualifiedMetadataName) =>
compilation.GetTypeByMetadataName(fullyQualifiedMetadataName)
?? throw new InvalidOperationException($"Type with metadata '{fullyQualifiedMetadataName}' not found");
IEnumerableType = GetTypeFromCompilationByMetadataNameOrThrow(compilation, IEnumerableMetadataName);
IServiceProviderType = GetTypeFromCompilationByMetadataNameOrThrow(compilation, IServiceProviderMetadataName);
IServiceScopeType = compilation.GetTypeByMetadataName(IServiceScopeMetadataName);
IAsyncDisposableType = compilation.GetTypeByMetadataName(IAsyncDisposableMetadataName);
IServiceScopeFactoryType = compilation.GetTypeByMetadataName(IServiceScopeFactoryMetadataName);
IServiceProviderIsServiceType = compilation.GetTypeByMetadataName(IServiceProviderIsServiceMetadataName);
IKeyedServiceProviderType = compilation.GetTypeByMetadataName(IKeyedServiceProviderMetadataName);
FromKeyedServicesAttribute = compilation.GetTypeByMetadataName(FromKeyedServicesAttributeMetadataName);
CompositionRootAttributeType =
GetTypeByMetadataNameOrThrow(assemblySymbol, CompositionRootAttributeMetadataName);
TransientAttributeType = GetTypeByMetadataNameOrThrow(assemblySymbol, TransientAttributeMetadataName);
GenericTransientAttributeType = assemblySymbol.GetTypeByMetadataName(GenericTransientAttributeMetadataName);
Generic2TransientAttributeType = assemblySymbol.GetTypeByMetadataName(Generic2TransientAttributeMetadataName);
SingletonAttribute = GetTypeByMetadataNameOrThrow(assemblySymbol, SingletonAttributeMetadataName);
GenericSingletonAttribute = assemblySymbol.GetTypeByMetadataName(GenericSingletonAttributeMetadataName);
Generic2SingletonAttribute = assemblySymbol.GetTypeByMetadataName(Generic2SingletonAttributeMetadataName);
ScopedAttribute = GetTypeByMetadataNameOrThrow(assemblySymbol, ScopedAttributeMetadataName);
GenericScopedAttribute = assemblySymbol.GetTypeByMetadataName(GenericScopedAttributeMetadataName);
Generic2ScopedAttribute = assemblySymbol.GetTypeByMetadataName(Generic2ScopedAttributeMetadataName);
ImportAttribute = GetTypeByMetadataNameOrThrow(assemblySymbol, ImportAttributeMetadataName);
GenericImportAttribute = assemblySymbol.GetTypeByMetadataName(GenericImportAttributeMetadataName);
ModuleAttribute = GetTypeByMetadataNameOrThrow(assemblySymbol, ServiceProviderModuleAttributeMetadataName);
FromNamedServicesAttribute = GetTypeByMetadataNameOrThrow(assemblySymbol, FromNamedServicesAttributeMetadataName);
ResolveDelegateType = GetTypeByMetadataNameOrThrow(assemblySymbol, ResolveDelegateMetadataName);
NamedResolveDelegateType = GetTypeByMetadataNameOrThrow(assemblySymbol, NamedResolveDelegateMetadataName);
}
public static bool HasKnownTypes(IModuleSymbol sourceModule)
{
return sourceModule.ReferencedAssemblySymbols.Any(s => s.Name == JabAttributesAssemblyName);
}
}