Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2a4bd6a

Browse files
committed
Use EmptyArray<T> instead of new T[0] in System.Composition code
1 parent 62bc5bd commit 2a4bd6a

File tree

12 files changed

+41
-10
lines changed

12 files changed

+41
-10
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Composition;
7+
using System.Diagnostics;
8+
using System.Diagnostics.Contracts;
9+
using System.Globalization;
10+
using System.Reflection;
11+
12+
namespace Microsoft.Internal
13+
{
14+
internal static class EmptyArray<T>
15+
{
16+
public static readonly T[] Value = new T[0];
17+
}
18+
}

src/System.Composition.Convention/src/System.Composition.Convention.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<DesignTime>True</DesignTime>
3333
<DependentUpon>CommonStrings.resx</DependentUpon>
3434
</Compile>
35+
<Compile Include="..\..\Common\src\Microsoft\Internal\EmptyArray.cs">
36+
<Link>Microsoft\Internal\EmptyArray.cs</Link>
37+
</Compile>
3538
<Compile Include="..\..\Common\src\Microsoft\Internal\Requires.cs">
3639
<Link>Microsoft\Internal\Requires.cs</Link>
3740
</Compile>

src/System.Composition.Convention/src/System/Composition/Convention/PartConventionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace System.Composition.Convention
1919
/// </summary>
2020
public class PartConventionBuilder
2121
{
22-
private readonly Type[] _emptyTypeArray = new Type[0];
22+
private readonly Type[] _emptyTypeArray = EmptyArray<Type>.Value;
2323
private static List<Attribute> s_onImportsSatisfiedAttributeList;
2424
private readonly static List<Attribute> s_importingConstructorList = new List<Attribute>() { new ImportingConstructorAttribute() };
2525
private readonly static Type s_exportAttributeType = typeof(ExportAttribute);

src/System.Composition.Hosting/src/System.Composition.Hosting.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<DesignTime>True</DesignTime>
4040
<DependentUpon>CommonStrings.resx</DependentUpon>
4141
</Compile>
42+
<Compile Include="..\..\Common\src\Microsoft\Internal\EmptyArray.cs">
43+
<Link>Microsoft\Internal\EmptyArray.cs</Link>
44+
</Compile>
4245
<Compile Include="..\..\Common\src\Microsoft\Internal\Requires.cs">
4346
<Link>Microsoft\Internal\Requires.cs</Link>
4447
</Compile>

src/System.Composition.Hosting/src/System/Composition/Hosting/CompositionHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace System.Composition.Hosting
2121
/// </summary>
2222
public sealed class CompositionHost : CompositionContext, IDisposable
2323
{
24-
private static readonly string[] s_noBoundaries = new string[0];
24+
private static readonly string[] s_noBoundaries = EmptyArray<string>.Value;
2525

2626
private readonly LifetimeContext _rootLifetimeContext;
2727

src/System.Composition.Hosting/src/System/Composition/Hosting/Core/ExportDescriptorRegistryUpdate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class ExportDescriptorRegistryUpdate : DependencyAccessor
1616
private readonly ExportDescriptorProvider[] _exportDescriptorProviders;
1717
private readonly IDictionary<CompositionContract, UpdateResult> _updateResults = new Dictionary<CompositionContract, UpdateResult>();
1818

19-
private static readonly CompositionDependency[] s_noDependenciesValue = new CompositionDependency[0];
19+
private static readonly CompositionDependency[] s_noDependenciesValue = EmptyArray<CompositionDependency>.Value;
2020
private static readonly Func<CompositionDependency[]> s_noDependencies = () => s_noDependenciesValue;
2121

2222
private bool _updateFinished;

src/System.Composition.Hosting/src/System/Composition/Hosting/Core/UpdateResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace System.Composition.Hosting.Core
1212
// providers, with providers being removed from the list before querying.
1313
internal class UpdateResult
1414
{
15-
private static readonly ExportDescriptorPromise[] s_noPromises = new ExportDescriptorPromise[0];
15+
private static readonly ExportDescriptorPromise[] s_noPromises = EmptyArray<ExportDescriptorPromise>.Value;
1616

1717
private readonly Queue<ExportDescriptorProvider> _remainingProviders;
1818
private readonly IList<ExportDescriptorPromise> _providedDescriptors = new List<ExportDescriptorPromise>();

src/System.Composition.Hosting/src/System/Composition/Hosting/Providers/ExportFactory/ExportFactoryExportDescriptorProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.Composition.Hosting.Util;
1010
using System.Composition.Hosting.Properties;
11+
using Microsoft.Internal;
1112

1213
namespace System.Composition.Hosting.Providers.ExportFactory
1314
{
@@ -28,14 +29,14 @@ public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(Compos
2829
private static ExportDescriptorPromise[] GetExportFactoryDescriptors<TProduct>(CompositionContract exportFactoryContract, DependencyAccessor definitionAccessor)
2930
{
3031
var productContract = exportFactoryContract.ChangeType(typeof(TProduct));
31-
var boundaries = new string[0];
32+
var boundaries = EmptyArray<string>.Value;
3233

3334
IEnumerable<string> specifiedBoundaries;
3435
CompositionContract unwrapped;
3536
if (exportFactoryContract.TryUnwrapMetadataConstraint(Constants.SharingBoundaryImportMetadataConstraintName, out specifiedBoundaries, out unwrapped))
3637
{
3738
productContract = unwrapped.ChangeType(typeof(TProduct));
38-
boundaries = (specifiedBoundaries ?? new string[0]).ToArray();
39+
boundaries = (specifiedBoundaries ?? EmptyArray<string>.Value).ToArray();
3940
}
4041

4142
return definitionAccessor.ResolveDependencies("product", productContract, false)

src/System.Composition.Hosting/src/System/Composition/Hosting/Providers/ExportFactory/ExportFactoryWithMetadataExportDescriptorProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Composition.Hosting.Providers.Metadata;
1212
using System.Composition.Hosting.Properties;
13+
using Microsoft.Internal;
1314

1415
namespace System.Composition.Hosting.Providers.ExportFactory
1516
{
@@ -33,14 +34,14 @@ public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(Compos
3334
private static ExportDescriptorPromise[] GetExportFactoryDescriptors<TProduct, TMetadata>(CompositionContract exportFactoryContract, DependencyAccessor definitionAccessor)
3435
{
3536
var productContract = exportFactoryContract.ChangeType(typeof(TProduct));
36-
var boundaries = new string[0];
37+
var boundaries = EmptyArray<string>.Value;
3738

3839
IEnumerable<string> specifiedBoundaries;
3940
CompositionContract unwrapped;
4041
if (exportFactoryContract.TryUnwrapMetadataConstraint(Constants.SharingBoundaryImportMetadataConstraintName, out specifiedBoundaries, out unwrapped))
4142
{
4243
productContract = unwrapped.ChangeType(typeof(TProduct));
43-
boundaries = (specifiedBoundaries ?? new string[0]).ToArray();
44+
boundaries = (specifiedBoundaries ?? EmptyArray<string>.Value).ToArray();
4445
}
4546

4647
var metadataProvider = MetadataViewProvider.GetMetadataViewProvider<TMetadata>();

src/System.Composition.TypedParts/src/System.Composition.TypedParts.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
</ProjectReference>
4040
</ItemGroup>
4141
<ItemGroup>
42+
<Compile Include="..\..\Common\src\Microsoft\Internal\EmptyArray.cs">
43+
<Link>Microsoft\Internal\EmptyArray.cs</Link>
44+
</Compile>
4245
<Compile Include="Properties\AssemblyInfo.cs" />
4346
<Compile Include="Properties\Resources.Designer.cs">
4447
<AutoGen>True</AutoGen>

0 commit comments

Comments
 (0)