Exemplo n.º 1
0
        internal static string GetPropertyName(AttributeFieldSource source)
        {
            var fieldName = source.FieldName;
            var propName  = (fieldName[0] == '_') ? fieldName.Substring(1) : fieldName;

            return(propName.ToUpperOnlyFirst());
        }
Exemplo n.º 2
0
        public void Execute(GeneratorExecutionContext context)
        {
            var attrCode = new MetaTagPropertyGeneratorAttributeTemplate().TransformText();

            context.AddSource(AttributeName + ".cs", attrCode);

            try
            {
                if (context.SyntaxReceiver is not SyntaxReceiver receiver)
                {
                    return;
                }

                foreach (var classDeclaration in receiver.Targets)
                {
                    var model      = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree);
                    var typeSymbol = model.GetDeclaredSymbol(classDeclaration);
                    if (typeSymbol is null)
                    {
                        continue;
                    }

                    var candidateFields = classDeclaration.Members.OfType <FieldDeclarationSyntax>()
                                          .Select(field => (field, model))
                                          .SelectMany(x => x.field.Declaration.Variables.Select(v => (x.field, symbol: x.model.GetDeclaredSymbol(v) as IFieldSymbol)))
                                          .Where(x => x.symbol?.GetAttributes().Any(y => y.AttributeClass?.Name is nameof(MetaTagPropertyGenerator) or AttributeName) ?? false)
                                          .ToArray();

                    var fieldSource = candidateFields.Select(x => AttributeFieldSource.Create(model, x.field)).ToArray();
                    if (fieldSource is null)
                    {
                        continue;
                    }

                    var template = new CodeTemplate(classDeclaration, fieldSource !)
                    {
                        Namespace = typeSymbol.ContainingNamespace.ToDisplayString(),
                    };

                    var text = template.TransformText();
                    context.AddSource(typeSymbol.GenerateHintName(), text);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 3
0
 internal static bool IsBuiltInType(AttributeFieldSource source) => source.FieldType == AttributeFieldSource.FieldDeclarationType.BuiltIn;
Exemplo n.º 4
0
 internal static string GetOptionId(AttributeFieldSource source) => "0x" + source.Id.ToString("x4");
Exemplo n.º 5
0
 internal static string GetOptionKey(AttributeFieldSource source) => source.Key;
Exemplo n.º 6
0
 internal static string GetMethodName(AttributeFieldSource source)
 => "GetTagValue_" + (source.FieldType == AttributeFieldSource.FieldDeclarationType.BuiltIn ? source.TypeName : "int");
Exemplo n.º 7
0
 internal static string GetLoadedFlagName(AttributeFieldSource source) => source.FieldName + "_loaded";
Exemplo n.º 8
0
 internal static string GetBackingFieldName(AttributeFieldSource source) => source.FieldName;
Exemplo n.º 9
0
 internal static string GetFieldTypeShortName(AttributeFieldSource source) => source.TypeName.Split('.').Last();
Exemplo n.º 10
0
 internal static string GetFieldTypeFullName(AttributeFieldSource source) => source.TypeName;