forked from DotNetAnalyzers/StyleCopAnalyzers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIArrayCreationOperationWrapper.g.cs
More file actions
56 lines (48 loc) · 2.63 KB
/
IArrayCreationOperationWrapper.g.cs
File metadata and controls
56 lines (48 loc) · 2.63 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
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
namespace StyleCop.Analyzers.Lightup
{
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
internal readonly struct IArrayCreationOperationWrapper
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IArrayCreationOperation";
private static readonly Type WrappedType;
private static readonly Func<IOperation, ImmutableArray<IOperation>> DimensionSizesAccessor;
private static readonly Func<IOperation, IOperation> InitializerAccessor;
private readonly IOperation operation;
static IArrayCreationOperationWrapper()
{
WrappedType = OperationWrapperHelper.GetWrappedType(typeof(IArrayCreationOperationWrapper));
DimensionSizesAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, ImmutableArray<IOperation>>(WrappedType, nameof(DimensionSizes));
InitializerAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Initializer));
}
private IArrayCreationOperationWrapper(IOperation operation)
{
this.operation = operation;
}
public IOperation WrappedOperation => this.operation;
public ITypeSymbol Type => this.WrappedOperation.Type;
public ImmutableArray<IOperation> DimensionSizes => DimensionSizesAccessor(this.WrappedOperation);
public IArrayInitializerOperationWrapper Initializer => IArrayInitializerOperationWrapper.FromOperation(InitializerAccessor(this.WrappedOperation));
public static explicit operator IArrayCreationOperationWrapper(IOperationWrapper wrapper) => FromOperation(wrapper.WrappedOperation);
public static implicit operator IOperationWrapper(IArrayCreationOperationWrapper wrapper) => IOperationWrapper.FromUpcast(wrapper.WrappedOperation);
public static IArrayCreationOperationWrapper FromOperation(IOperation operation)
{
if (operation == null)
{
return default;
}
if (!IsInstance(operation))
{
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'");
}
return new IArrayCreationOperationWrapper(operation);
}
public static bool IsInstance(IOperation operation)
{
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType);
}
}
}