internal static string GetPropertyName(AttributeFieldSource source) { var fieldName = source.FieldName; var propName = (fieldName[0] == '_') ? fieldName.Substring(1) : fieldName; return(propName.ToUpperOnlyFirst()); }
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()); } }
internal static bool IsBuiltInType(AttributeFieldSource source) => source.FieldType == AttributeFieldSource.FieldDeclarationType.BuiltIn;
internal static string GetOptionId(AttributeFieldSource source) => "0x" + source.Id.ToString("x4");
internal static string GetOptionKey(AttributeFieldSource source) => source.Key;
internal static string GetMethodName(AttributeFieldSource source) => "GetTagValue_" + (source.FieldType == AttributeFieldSource.FieldDeclarationType.BuiltIn ? source.TypeName : "int");
internal static string GetLoadedFlagName(AttributeFieldSource source) => source.FieldName + "_loaded";
internal static string GetBackingFieldName(AttributeFieldSource source) => source.FieldName;
internal static string GetFieldTypeShortName(AttributeFieldSource source) => source.TypeName.Split('.').Last();
internal static string GetFieldTypeFullName(AttributeFieldSource source) => source.TypeName;