/// <summary> /// Initializes a new instance of the <see cref="PropertyInfo"/> class. /// </summary> /// <param name="property">The property.</param> public PropertyInfo(IPropertySymbol property) { this.Name = property.Name; var attributeConstructorArguments = property .GetAttributes("CompiledAttribute", "CompiledDictionaryAttribute") .SelectMany(a => a.ConstructorArguments); var attributeNamedArguments = property .GetAttributes("CompiledAttribute", "CompiledDictionaryAttribute") .SelectMany(a => a.NamedArguments); this.ReturnType = attributeConstructorArguments .First(x => x.Type?.Name == "Type").Value?.ToString() ?? "void"; Trace.WriteLine(string.Join(", ", property.GetAttributes("CompiledVariableAttribute").Select(a => a.ConstructorArguments.Select(x => x.Type)))); this.Variables = property.GetAttributes("CompiledVariableAttribute") .Select(a => ( a.ConstructorArguments.First(x => x.Type?.Name.Equals("String", StringComparison.OrdinalIgnoreCase) == true).Value?.ToString() ?? "void", a.ConstructorArguments.First(x => x.Type?.Name.Equals("Type", StringComparison.OrdinalIgnoreCase) == true).Value?.ToString() ?? "void")).ToList(); this.AsFormattedText = attributeNamedArguments .FirstOrDefault(a => a.Key.Equals("AsFormattedText")) .Value.Value?.ToString().Equals(bool.TrueString) ?? false; this.Protected = attributeNamedArguments .FirstOrDefault(a => a.Key.Equals("Protected")) .Value.Value?.ToString().Equals(bool.TrueString) ?? false; }
public static string GetTraceType(this IPropertySymbol symbol) { if (symbol.GetAttributes().FirstOrDefault() != null) { return(((symbol.GetAttributes()[0].ConstructorArguments).FirstOrDefault()).Value.ToString()); } if (symbol.ContainingType.GetAttributes().FirstOrDefault() != null) { return(((symbol.ContainingType.GetAttributes()[0].ConstructorArguments).FirstOrDefault()).Value.ToString()); } throw new ArgumentException("Can't find trace type."); }
public void OnVisitSyntaxNode(GeneratorSyntaxContext context) { if (context.Node is PropertyDeclarationSyntax propertyDeclarationSyntax && propertyDeclarationSyntax.AttributeLists.Any()) { ClassDeclarationSyntax classDeclarationSyntax = (ClassDeclarationSyntax)propertyDeclarationSyntax.Parent; if (!classDeclarationSyntax.Modifiers.Any(k => k.IsKind(SyntaxKind.PartialKeyword))) { return; } IPropertySymbol propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax); INamedTypeSymbol @class = propertySymbol.ContainingType; if (@class.GetMembers().Any(m => m.Name == "SetParametersAsync")) { return; } if (propertySymbol.GetAttributes().Any(ad => ad.AttributeClass.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" || ad.AttributeClass.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute")) { Properties.Add(propertySymbol); } } }
private static async Task CheckDacPropertyAsync(IPropertySymbol property, SymbolAnalysisContext symbolContext, PXContext pxContext, FieldTypeAttributesRegister fieldAttributesRegister) { symbolContext.CancellationToken.ThrowIfCancellationRequested(); ImmutableArray <AttributeData> attributes = property.GetAttributes(); if (attributes.Length == 0) { return; } var attributesWithInfos = GetFieldTypeAttributesInfos(pxContext, attributes, fieldAttributesRegister, symbolContext.CancellationToken); if (attributesWithInfos.Count == 0) { return; } bool validSpecialTypes = await CheckForMultipleSpecialAttributesAsync(symbolContext, attributesWithInfos).ConfigureAwait(false); symbolContext.CancellationToken.ThrowIfCancellationRequested(); if (!validSpecialTypes) { return; } await CheckForFieldTypeAttributesAsync(property, symbolContext, pxContext, attributesWithInfos) .ConfigureAwait(false); }
public override MetadataItem VisitProperty(IPropertySymbol symbol) { var item = GetMetadataItem(symbol); if (item == null) { return(null); } item.EnsureSyntax(); item.Syntax.Content = symbol.GenerateSyntax(item.Type, ApiFilter); item.Syntax.Parameters = symbol.Parameters .Select(x => CreateParameter(x, item)) .ToList(); item.Syntax.Return = CreateParameter(symbol, item, true); item.Modifiers = symbol.GeneratePropertyModifiers(); item.Implements = symbol.GetMemberImplements(ApiFilter); item.Attributes = GetAttributeInfo(symbol.GetAttributes()); item.IsExplicitInterfaceImplementation = !symbol.ExplicitInterfaceImplementations.IsEmpty; return(item); }
public static IPropertySymbol RenameParameters(this IPropertySymbol property, ImmutableArray <string> parameterNames) { var parameterList = property.Parameters; if (parameterList.Select(p => p.Name).SequenceEqual(parameterNames)) { return(property); } var parameters = parameterList.RenameParameters(parameterNames); return(CodeGenerationSymbolFactory.CreatePropertySymbol( property.ContainingType, property.GetAttributes(), property.DeclaredAccessibility, property.GetSymbolModifiers(), property.Type, property.RefKind, property.ExplicitInterfaceImplementations, property.Name, parameters, property.GetMethod, property.SetMethod, property.IsIndexer)); }
public static IPropertySymbol RemoveInaccessibleAttributesAndAttributesOfTypes( this IPropertySymbol property, ISymbol accessibleWithin, params INamedTypeSymbol[] attributesToRemove) { bool shouldRemoveAttribute(AttributeData a) => attributesToRemove.Any(attr => attr.Equals(a.AttributeClass)) || !a.AttributeClass.IsAccessibleWithin(accessibleWithin); var someParameterHasAttribute = property.Parameters .Any(p => p.GetAttributes().Any(shouldRemoveAttribute)); if (!someParameterHasAttribute) { return(property); } return(CodeGenerationSymbolFactory.CreatePropertySymbol( property.ContainingType, property.GetAttributes(), property.DeclaredAccessibility, property.GetSymbolModifiers(), property.Type, property.RefKind, property.ExplicitInterfaceImplementations, property.Name, property.Parameters.SelectAsArray(p => CodeGenerationSymbolFactory.CreateParameterSymbol( p.GetAttributes().WhereAsArray(a => !shouldRemoveAttribute(a)), p.RefKind, p.IsParams, p.Type, p.Name, p.IsOptional, p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)), property.GetMethod, property.SetMethod, property.IsIndexer)); }
public static string GetInversePropertyName(this IPropertySymbol prop) { if (prop == null) { throw new ArgumentNullException(nameof(prop)); } AttributeData inversePropertyAtt = prop.GetAttributes().SingleOrDefault(att => att.AttributeClass.Name == "InversePropertyAttribute"); if (inversePropertyAtt != null) { return(inversePropertyAtt.ConstructorArguments.Single().Value.ToString()); } else { ITypeSymbol thisClass = prop.ContainingType; ITypeSymbol otherClass = prop.Type.IsCollectionType() ? prop.Type.GetElementType() : prop.Type; string?otherPropName = otherClass .GetMembers() .OfType <IPropertySymbol>() .Select(p => new { p.Name, p.Type }) .Select(p => new { p.Name, Type = p.Type.IsCollectionType() ? p.Type.GetElementType() : p.Type }) .ExtendedSingleOrDefault($"Finding inverse property for {prop.Name} of {thisClass.Name}", p => p.Type == thisClass)?.Name; return(otherPropName ?? "$$unbound"); } }
private static bool AnalyzePropertyAttributes(IPropertySymbol propertySymbol) { foreach (AttributeData attributeData in propertySymbol.GetAttributes()) { INamedTypeSymbol attributeClass = attributeData.AttributeClass; if (string.Equals(attributeClass.Name, "DependencyAttribute", StringComparison.Ordinal)) { return(false); } if (attributeClass.HasMetadataName(MetadataNames.System_Runtime_Serialization_DataMemberAttribute)) { return(false); } if (attributeClass.HasMetadataName(Microsoft_AspNetCore_Components_ParameterAttribute)) { return(false); } if (attributeClass.HasMetadataName(Microsoft_AspNetCore_Components_CascadingParameterAttribute)) { return(false); } if (attributeClass.HasMetadataName(Newtonsoft_Json_JsonPropertyAttribute)) { return(false); } } return(true); }
private static MemberDeclarationSyntax GenerateIndexerDeclaration( IPropertySymbol property, CodeGenerationDestination destination, CodeGenerationOptions options, ParseOptions parseOptions ) { var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier( property.ExplicitInterfaceImplementations ); var declaration = SyntaxFactory.IndexerDeclaration( attributeLists: AttributeGenerator.GenerateAttributeLists( property.GetAttributes(), options ), modifiers: GenerateModifiers(property, destination, options), type: GenerateTypeSyntax(property), explicitInterfaceSpecifier: explicitInterfaceSpecifier, parameterList: ParameterGenerator.GenerateBracketedParameterList( property.Parameters, explicitInterfaceSpecifier != null, options ), accessorList: GenerateAccessorList(property, destination, options, parseOptions) ); declaration = UseExpressionBodyIfDesired(options, declaration, parseOptions); return(AddFormatterAndCodeGeneratorAnnotationsTo( AddAnnotationsTo(property, declaration) )); }
private string GetPropertyName(IPropertySymbol propertySymbol) { var name = propertySymbol.Name; var attributes = propertySymbol.GetAttributes(); var jsonPropertyAttribute = attributes .FirstOrDefault(a => a.AttributeClass.Name == "JsonPropertyAttribute"); if (jsonPropertyAttribute != null) { if (jsonPropertyAttribute.ConstructorArguments.Length > 0) { var firstArgument = jsonPropertyAttribute.ConstructorArguments.First(); if (firstArgument.Type.SpecialType == SpecialType.System_String) { name = (string)firstArgument.Value; } } } var dataMemberAttribute = attributes.FirstOrDefault(a => a.AttributeClass.Name == "DataMemberAttribute"); if (dataMemberAttribute != null) { var namedArguments = dataMemberAttribute.NamedArguments; if (namedArguments.Length > 0 && namedArguments.Any(p => p.Key == "Name")) { var nameArgument = namedArguments.First(p => p.Key == "Name"); if (nameArgument.Value.Type.SpecialType == SpecialType.System_String) { name = (string)nameArgument.Value.Value; } } } return(name); }
private void ConfigureBoundAttribute( BoundAttributeDescriptorBuilder builder, IPropertySymbol property, INamedTypeSymbol containingType) { var attributeNameAttribute = property .GetAttributes() .Where(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, _htmlAttributeNameAttributeSymbol)) .FirstOrDefault(); bool hasExplicitName; string attributeName; if (attributeNameAttribute == null || attributeNameAttribute.ConstructorArguments.Length == 0 || string.IsNullOrEmpty((string)attributeNameAttribute.ConstructorArguments[0].Value)) { hasExplicitName = false; attributeName = HtmlConventions.ToHtmlCase(property.Name); } else { hasExplicitName = true; attributeName = (string)attributeNameAttribute.ConstructorArguments[0].Value; } var hasPublicSetter = property.SetMethod != null && property.SetMethod.DeclaredAccessibility == Accessibility.Public; var typeName = GetFullName(property.Type); builder.TypeName = typeName; builder.SetPropertyName(property.Name); if (hasPublicSetter) { builder.Name = attributeName; if (property.Type.TypeKind == TypeKind.Enum) { builder.IsEnum = true; } if (IncludeDocumentation) { var xml = property.GetDocumentationCommentXml(); if (!string.IsNullOrEmpty(xml)) { builder.Documentation = xml; } } } else if (hasExplicitName && !IsPotentialDictionaryProperty(property)) { // Specified HtmlAttributeNameAttribute.Name though property has no public setter. var diagnostic = RazorDiagnosticFactory.CreateTagHelper_InvalidAttributeNameNullOrEmpty(GetFullName(containingType), property.Name); builder.Diagnostics.Add(diagnostic); } ConfigureDictionaryBoundAttribute(builder, property, containingType, attributeNameAttribute, attributeName, hasPublicSetter); }
internal static bool IsModelProperty(this IPropertySymbol propertySymbol, INamedTypeSymbol ignoreAttributeType) { if (propertySymbol is null) { return(false); } if (propertySymbol.GetMethod is null || propertySymbol.SetMethod is null) { return(false); } if (!propertySymbol.IsVirtual) { return(false); } if (propertySymbol.DeclaredAccessibility != Accessibility.Public) { return(false); } if (ignoreAttributeType != null) { var attribute = propertySymbol.GetAttributes().FirstOrDefault(attr => ignoreAttributeType.IsAssignableFrom(attr.AttributeClass)); if (attribute != null) { return(false); } } return(true); }
private static MemberDeclarationSyntax GeneratePropertyDeclaration( IPropertySymbol property, CodeGenerationDestination destination, Workspace workspace, CodeGenerationOptions options, ParseOptions parseOptions) { var initializer = CodeGenerationPropertyInfo.GetInitializer(property) is ExpressionSyntax initializerNode ? SyntaxFactory.EqualsValueClause(initializerNode) : null; var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations); var accessorList = GenerateAccessorList(property, destination, workspace, options, parseOptions); var propertyDeclaration = SyntaxFactory.PropertyDeclaration( attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options), modifiers: GenerateModifiers(property, destination, options), type: GenerateTypeSyntax(property), explicitInterfaceSpecifier: explicitInterfaceSpecifier, identifier: property.Name.ToIdentifierToken(), accessorList: accessorList, expressionBody: null, initializer: initializer); propertyDeclaration = UseExpressionBodyIfDesired( workspace, propertyDeclaration, parseOptions); return(AddFormatterAndCodeGeneratorAnnotationsTo( AddAnnotationsTo(property, propertyDeclaration))); }
private async Task IsDBFieldPropertyAsync(string source, List <bool> expected, string[] code = null) { Document document = CreateDocument(source, externalCode: code); SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false); var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); List <bool> actual = new List <bool>(capacity: expected.Capacity); var pxContext = new PXContext(semanticModel.Compilation, CodeAnalysisSettings.Default); var attributeInformation = new Acuminator.Utilities.Roslyn.PXFieldAttributes.AttributeInformation(pxContext); IEnumerable <PropertyDeclarationSyntax> properties = syntaxRoot.DescendantNodes() .OfType <PropertyDeclarationSyntax>() .Where(a => !a.AttributeLists.IsNullOrEmpty()); foreach (PropertyDeclarationSyntax property in properties) { IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(property); actual.Add((propertySymbol != null)? attributeInformation.ContainsBoundAttributes(propertySymbol.GetAttributes()): false); } actual.Should().BeEquivalentTo(expected); }
private static bool IsObservedCollectionType(IPropertySymbol propertySymbol) => !propertySymbol.IsOverride && !propertySymbol.GetAttributes(KnownType.System_Runtime_Serialization_DataMemberAttribute).Any() && !propertySymbol.ContainingType.GetAttributes(KnownType.System_SerializableAttribute).Any() && propertySymbol.Type.OriginalDefinition.DerivesOrImplementsAny(collectionTypes) && !propertySymbol.Type.OriginalDefinition.DerivesOrImplementsAny(ignoredCollectionTypes) && !IsInterfaceImplementation(propertySymbol);
public static IPropertySymbol RemoveAttributeFromParameters( this IPropertySymbol property, INamedTypeSymbol attributeType) { if (attributeType == null) { return(property); } var someParameterHasAttribute = property.Parameters .Any(p => p.GetAttributes().Any(a => a.AttributeClass.Equals(attributeType))); if (!someParameterHasAttribute) { return(property); } return(CodeGenerationSymbolFactory.CreatePropertySymbol( property.ContainingType, property.GetAttributes(), property.DeclaredAccessibility, property.GetSymbolModifiers(), property.Type, property.ExplicitInterfaceImplementations.FirstOrDefault(), property.Name, property.Parameters.Select(p => CodeGenerationSymbolFactory.CreateParameterSymbol( p.GetAttributes().Where(a => !a.AttributeClass.Equals(attributeType)).ToList(), p.RefKind, p.IsParams, p.Type, p.Name, p.IsOptional, p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)).ToList(), property.GetMethod, property.SetMethod, property.IsIndexer)); }
private bool InternalIsAutoProperty(IPropertySymbol property) { var attr = property.GetAttributes().FirstOrDefault(a => a.AttributeClass.Equals(GetPhaseType("Phase.Attributes.AutoPropertyAttribute"))); if (attr != null) { return true; } if (property.ContainingType.TypeKind != TypeKind.Class && property.ContainingType.TypeKind != TypeKind.Struct) { return false; } if (property.IsAbstract || property.IsExtern) { return false; } if (IsInterfaceImplementation(property, out var interfaceMember)) { return IsAutoProperty((IPropertySymbol)interfaceMember); } var declaration = (BasePropertyDeclarationSyntax)property.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax(); if (declaration != null) { return IsAutoProperty(declaration); } return false; }
public static bool IsParameterWithCaptureUnmatchedValues(ComponentSymbols symbols, IPropertySymbol property) { if (symbols == null) { throw new ArgumentNullException(nameof(symbols)); } if (property == null) { throw new ArgumentNullException(nameof(property)); } var attribute = property.GetAttributes().FirstOrDefault(a => a.AttributeClass == symbols.ParameterAttribute); if (attribute == null) { return(false); } foreach (var kvp in attribute.NamedArguments) { if (string.Equals(kvp.Key, ComponentsApi.ParameterAttribute.CaptureUnmatchedValues, StringComparison.Ordinal)) { return(kvp.Value.Value as bool? ?? false); } } return(false); }
public override MetadataItem VisitProperty(IPropertySymbol symbol) { MetadataItem result = GetYamlItem(symbol); if (result == null) { return(null); } if (result.Syntax == null) { result.Syntax = new SyntaxDetail { Content = new SortedList <SyntaxLanguage, string>() }; } if (result.Syntax.Parameters == null) { result.Syntax.Parameters = new List <ApiParameter>(); } if (result.Syntax.Content == null) { result.Syntax.Content = new SortedList <SyntaxLanguage, string>(); } _generator.GenerateSyntax(result.Type, symbol, result.Syntax, this); var typeGenericParameters = symbol.ContainingType.IsGenericType ? symbol.ContainingType.Accept(TypeGenericParameterNameVisitor.Instance) : EmptyListOfString; if (symbol.Parameters.Length > 0) { foreach (var p in symbol.Parameters) { var id = AddSpecReference(p.Type, typeGenericParameters); var param = VisitorHelper.GetParameterDescription(p, result, id, false, GetTripleSlashCommentParserContext(result, _preserveRawInlineComments)); Debug.Assert(param.Type != null); result.Syntax.Parameters.Add(param); } } { var id = AddSpecReference(symbol.Type, typeGenericParameters); result.Syntax.Return = VisitorHelper.GetParameterDescription(symbol, result, id, true, GetTripleSlashCommentParserContext(result, _preserveRawInlineComments)); Debug.Assert(result.Syntax.Return.Type != null); } if (symbol.IsOverride && symbol.OverriddenProperty != null) { result.Overridden = AddSpecReference(symbol.OverriddenProperty, typeGenericParameters); } result.Overload = AddOverloadReference(symbol.OriginalDefinition); _generator.GenerateProperty(symbol, result, this); AddMemberImplements(symbol, result, typeGenericParameters); result.Attributes = GetAttributeInfo(symbol.GetAttributes()); result.IsExplicitInterfaceImplementation = !symbol.ExplicitInterfaceImplementations.IsEmpty; return(result); }
public static bool HasConcurrencyCheck(this IPropertySymbol prop) { if (prop == null) { throw new ArgumentNullException(nameof(prop)); } return(prop.GetAttributes().Any(att => att.AttributeClass.Name == "ConcurrencyCheckAttribute")); }
private static bool IsObservedCollectionType(IPropertySymbol propertySymbol) { var hasDataMemberAttribute = propertySymbol.GetAttributes().Any(attribute => attribute.AttributeClass.Is(KnownType.System_Runtime_Serialization_DataMemberAttribute)); return(!hasDataMemberAttribute && propertySymbol.Type.OriginalDefinition.DerivesOrImplementsAny(collectionTypes) && !propertySymbol.Type.OriginalDefinition.DerivesOrImplementsAny(ignoredCollectionTypes)); }
protected static AttributeData GetConstructorArgumentAttributeOrDefault(IPropertySymbol propertySymbol) { var attributes = propertySymbol.GetAttributes(KnownType.System_Windows_Markup_ConstructorArgumentAttribute) .ToList(); return(attributes.Count == 1 ? attributes[0] : null); }
private static bool IsKeyByConfiguration(this IPropertySymbol prop) { if (prop == null) { throw new ArgumentNullException(nameof(prop)); } return(prop.GetAttributes().Any(att => att.AttributeClass.Name == "KeyAttribute")); }
public static bool IsProtobufNetProperty(this IPropertySymbol symbol) { var attr = symbol.GetAttributes().FirstOrDefault(); if (attr != null && attr.AttributeClass.ToString() == "ProtoBuf.ProtoMemberAttribute") { return(true); } return(false); }
public override IList <CustomAttributeData> GetCustomAttributesData() { var attributes = new List <CustomAttributeData>(); foreach (AttributeData a in _property.GetAttributes()) { attributes.Add(new CustomAttributeDataWrapper(a, _metadataLoadContext)); } return(attributes); }
private static void AnalyzeDeclaration(SymbolAnalysisContext context, INamedTypeSymbol dbType, IPropertySymbol dbTable, HashSet <string> names) { var compilation = context.Compilation; var foreignKeyAttribute = compilation.GetKnownType(KnownTypes.RelationshipAttribute); var attributes = dbTable.GetAttributes().Where(x => foreignKeyAttribute.Equals(x.AttributeClass)).ToImmutableArray(); for (int i = 0; i < attributes.Length; i++) { AnalyzeDeclaration(context, dbType, dbTable, attributes[i], names); } }
private AttributeData GetConstructorArgumentAttributeOrDefault(IPropertySymbol propertySymbol) { var attributes = propertySymbol.GetAttributes() .Where(attribute => attribute.AttributeClass.Is(KnownType.System_Windows_Markup_ConstructorArgumentAttribute)) .ToList(); return(attributes.Count == 1 ? attributes[0] : null); }
BindableInfo GetBindableInfo(IPropertySymbol property) { return(property.GetAttributes() .FirstOrDefault(x => x.AttributeClass == bindablePropertyAttributeType) .With(x => { var args = x.ConstructorArguments.Select(arg => arg.Value).ToArray(); var namedArgs = x.NamedArguments.ToImmutableDictionary(p => p.Key, p => (string)p.Value.Value); //TODO error if names are not recognizable return new BindableInfo(args.Length > 0 ? (bool)args[0] : true, namedArgs.GetValueOrDefault("OnPropertyChangedMethodName"), namedArgs.GetValueOrDefault("OnPropertyChangingMethodName")); })); }
public PropertySymbolInfo(IPropertySymbol p) { ContainingType = new TypeSymbolInfo(p.ContainingType); RealProperty = p; Type = new TypeSymbolInfo(p.Type); Accessibility = p.DeclaredAccessibility.ConvertToStructure(); GetMethod = CanRead ? new MethodSymbolInfo(p.GetMethod) : null; SetMethod = CanWrite ? new MethodSymbolInfo(p.SetMethod) : null; Parameters = EnumerableExtensions.CreateLazyImmutableArray <IParameterSymbolInfo>(() => RealProperty.Parameters.Select(i => new ParameterSymbolInfo(i))); Attributes = EnumerableExtensions.CreateLazyImmutableArray(() => RealProperty.GetAttributes() .Select(AotSymbolExtensions.ConvertToStructure) .Where(i => i != null)); }
private static MemberDeclarationSyntax GenerateIndexerDeclaration( IPropertySymbol property, CodeGenerationDestination destination, CodeGenerationOptions options) { var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations); return AddCleanupAnnotationsTo( AddAnnotationsTo(property, SyntaxFactory.IndexerDeclaration( attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options), modifiers: GenerateModifiers(property, destination, options), type: property.Type.GenerateTypeSyntax(), explicitInterfaceSpecifier: explicitInterfaceSpecifier, parameterList: ParameterGenerator.GenerateBracketedParameterList(property.Parameters, explicitInterfaceSpecifier != null, options), accessorList: GenerateAccessorList(property, destination, options)))); }
public static MemberDeclarationSyntax GeneratePropertyDeclaration( IPropertySymbol property, CodeGenerationDestination destination, CodeGenerationOptions options) { var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations); return AddCleanupAnnotationsTo( AddAnnotationsTo(property, SyntaxFactory.PropertyDeclaration( attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options), modifiers: GenerateModifiers(property, destination, options), type: property.Type.GenerateTypeSyntax(), explicitInterfaceSpecifier: explicitInterfaceSpecifier, identifier: property.Name.ToIdentifierToken(), accessorList: GenerateAccessorList(property, destination, options)))); }
private ControlPropertyMetadata GetPropertyMetadata(IPropertySymbol property) { var attribute = property.GetAttributes().FirstOrDefault(a => CheckType(a.AttributeClass, typeof(MarkupOptionsAttribute))); var metadata = new ControlPropertyMetadata() { Type = property.Type, IsTemplate = CheckType((INamedTypeSymbol)property.Type, typeof(ITemplate)) || property.Type.AllInterfaces.Any(i => CheckType(i, typeof(ITemplate))), AllowHtmlContent = property.Type.AllInterfaces.Any(i => CheckType(i, typeof(IControlWithHtmlAttributes))) }; if (attribute != null) { metadata.Name = attribute.NamedArguments.Where(a => a.Key == "Name").Select(a => a.Value.Value as string).FirstOrDefault() ?? property.Name; metadata.AllowBinding = attribute.NamedArguments.Where(a => a.Key == "AllowBinding").Select(a => a.Value.Value as bool?).FirstOrDefault() ?? true; metadata.AllowHardCodedValue = attribute.NamedArguments.Where(a => a.Key == "AllowHardCodedValue").Select(a => a.Value.Value as bool?).FirstOrDefault() ?? true; var mappingMode = (MappingMode)(attribute.NamedArguments.Where(a => a.Key == "MappingMode").Select(a => a.Value.Value as int?).FirstOrDefault() ?? 0); if (mappingMode == MappingMode.InnerElement) { metadata.IsElement = true; } else if (mappingMode == MappingMode.Exclude) { return null; } } else { metadata.Name = property.Name; metadata.AllowBinding = true; metadata.AllowHardCodedValue = true; } if (metadata.IsTemplate) { metadata.IsElement = true; } return metadata; }
private static bool IsExcludedFromMapping(IPropertySymbol property, ITypeSymbol metadataClass) { if (metadataClass != null) { property = (metadataClass.GetMembers(property.Name).FirstOrDefault() as IPropertySymbol) ?? property; } return property .GetAttributes() .Any(a => a.AttributeClass.ToString() == "NCR.Engage.RoslynAnalysis.ExcludeFromMappingAttribute"); }
private static MemberDeclarationSyntax GeneratePropertyDeclaration( IPropertySymbol property, CodeGenerationDestination destination, CodeGenerationOptions options) { var initializerNode = CodeGenerationPropertyInfo.GetInitializer(property) as ExpressionSyntax; var initializer = initializerNode != null ? SyntaxFactory.EqualsValueClause(initializerNode) : default(EqualsValueClauseSyntax); var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations); var accessorList = GenerateAccessorList(property, destination, options); return AddCleanupAnnotationsTo( AddAnnotationsTo(property, SyntaxFactory.PropertyDeclaration( attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options), modifiers: GenerateModifiers(property, destination, options), type: property.Type.GenerateTypeSyntax(), explicitInterfaceSpecifier: explicitInterfaceSpecifier, identifier: property.Name.ToIdentifierToken(), accessorList: accessorList, expressionBody: default(ArrowExpressionClauseSyntax), initializer: initializer))); }
private void ValidateProperty(IPropertySymbol property, out bool isCollection, out ITypeSymbol scalarType) { if (!IsSupportedPropertyType(property.Type, out isCollection, out scalarType)) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, "Invalid property: {0} - the property type {1} is not supported by Entity Framework.", property.ToDisplayString(this.ErrorMessageDisplayFormat), property.Type.ToDisplayString(this.ErrorMessageDisplayFormat))); } var inversePropertyAttributeType = compilation.GetTypeByMetadataName(Constants.InversePropertyAttribute); var inversePropertyAttribute = property.GetAttributes().SingleOrDefault(x => x.AttributeClass == inversePropertyAttributeType); if (inversePropertyAttribute != null) { var scalarTypeCopy = scalarType; var targetType = this.interfaceSymbols.SingleOrDefault(x => x == scalarTypeCopy); if (targetType == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, "Invalid {0} attribute on property {1}. The property type {2} must be marked as an Entity.", inversePropertyAttributeType.ToDisplayString(this.ErrorMessageDisplayFormat), property.ToDisplayString(this.ErrorMessageDisplayFormat), scalarType.ToDisplayString(this.ErrorMessageDisplayFormat))); } var inversePropertyName = (string)inversePropertyAttribute.ConstructorArguments[0].Value; var targetProperty = targetType .GetMembers() .OfType<IPropertySymbol>() .Where(x => x.Name == inversePropertyName) .FirstOrDefault(); if (targetProperty == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, "Invalid {0} attribute on property {1}. A property named '{2}' cannot be found on the target interface type {3}.", inversePropertyAttributeType.ToDisplayString(this.ErrorMessageDisplayFormat), property.ToDisplayString(this.ErrorMessageDisplayFormat), inversePropertyName, targetType.ToDisplayString(this.ErrorMessageDisplayFormat))); } } }
private bool IsIgnoredProperty(IPropertySymbol property) { var ignoreAttributeType = compilation.GetTypeByMetadataName(Constants.IgnoreAttribute); return property .GetAttributes() .Any(x => x.AttributeClass == ignoreAttributeType); }
BindableInfo GetBindableInfo(IPropertySymbol property) { return property.GetAttributes() .FirstOrDefault(x => x.AttributeClass == bindablePropertyAttributeType) .With(x => { var args = x.ConstructorArguments.Select(arg => arg.Value).ToArray(); var namedArgs = x.NamedArguments.ToImmutableDictionary(p => p.Key, p => (string)p.Value.Value); //TODO error if names are not recognizable return new BindableInfo(args.Length > 0 ? (bool)args[0] : true, namedArgs.GetValueOrDefault("OnPropertyChangedMethodName"), namedArgs.GetValueOrDefault("OnPropertyChangingMethodName")); }); }