private static object ConvertTypedConstantValue( Type type, TypedConstant constructorArgument) { switch (constructorArgument.Kind) { case TypedConstantKind.Enum: return(Enum.ToObject(type, constructorArgument.Value)); case TypedConstantKind.Primitive: return(constructorArgument.Value); case TypedConstantKind.Type: var typeSymbol = (INamedTypeSymbol)constructorArgument.Value; var typeName = CodeAnalysisSymbolBasedTypeInfo.GetAssemblyQualifiedName(typeSymbol); return(Type.GetType(typeName)); case TypedConstantKind.Array: Debug.Assert(type.IsArray && constructorArgument.Values != null); var elementType = type.GetElementType(); var values = Array.CreateInstance(elementType, constructorArgument.Values.Length); for (var index = 0; index < values.Length; index++) { values.SetValue( ConvertTypedConstantValue(elementType, constructorArgument.Values[index]), index); } return(values); default: throw new NotSupportedException( Resources.FormatCodeAnalysis_TypeConstantKindNotSupported(constructorArgument.Kind)); } }
/// <summary> /// Initializes a new instance of <see cref="CodeAnalysisSymbolBasedPropertyInfo"/>. /// </summary> /// <param name="propertySymbol">The <see cref="IPropertySymbol"/>.</param> public CodeAnalysisSymbolBasedPropertyInfo(IPropertySymbol propertySymbol) { if (propertySymbol == null) { throw new ArgumentNullException(nameof(propertySymbol)); } _propertySymbol = propertySymbol; PropertyType = new CodeAnalysisSymbolBasedTypeInfo(_propertySymbol.Type); }
private static ConstructorInfo FindConstructor( Type type, ImmutableArray <TypedConstant> symbolConstructorArguments) { var constructors = type.GetConstructors(); foreach (var constructor in constructors) { var runtimeParameters = constructor.GetParameters(); if (runtimeParameters.Length != symbolConstructorArguments.Length) { continue; } var parametersMatched = true; for (var index = 0; index < runtimeParameters.Length; index++) { var runtimeParameter = runtimeParameters[index].ParameterType; if (symbolConstructorArguments[index].Kind == TypedConstantKind.Array && runtimeParameter.IsArray) { var arrayType = (IArrayTypeSymbol)symbolConstructorArguments[index].Type; if (!CodeAnalysisSymbolBasedTypeInfo.IsType( arrayType.ElementType, runtimeParameter.GetElementType().GetTypeInfo())) { parametersMatched = false; break; } } else { if (!CodeAnalysisSymbolBasedTypeInfo.IsType( symbolConstructorArguments[index].Type, runtimeParameter.GetTypeInfo())) { parametersMatched = false; break; } } } if (parametersMatched) { return(constructor); } } throw new InvalidOperationException(Resources.FormatCodeAnalysisConstructorNotFound(type.FullName)); }
/// <summary> /// Gets the sequence of <see cref="Attribute"/>s of type <typeparamref name="TAttribute"/> /// that are declared on the specified <paramref name="symbol"/>. /// </summary> /// <typeparam name="TAttribute">The <see cref="Attribute"/> type.</typeparam> /// <param name="symbol">The <see cref="ISymbol"/> to find attributes on.</param> /// <returns></returns> public static IEnumerable <TAttribute> GetCustomAttributes <TAttribute>(ISymbol symbol) where TAttribute : Attribute { if (symbol == null) { throw new ArgumentNullException(nameof(symbol)); } var attributes = symbol.GetAttributes(); if (attributes.Length > 0) { return(attributes .Where(attribute => CodeAnalysisSymbolBasedTypeInfo.IsType( attribute.AttributeClass, typeof(TAttribute).GetTypeInfo())) .Select(attribute => CreateAttribute <TAttribute>(attribute)) .ToArray()); } return(Enumerable.Empty <TAttribute>()); }