PropertyMetadata.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Microsoft.CodeAnalysis;
  2. namespace Lua.SourceGenerator;
  3. public class PropertyMetadata
  4. {
  5. public ISymbol Symbol { get; }
  6. public ITypeSymbol Type { get; }
  7. public string TypeFullName { get; }
  8. public bool IsStatic { get; }
  9. public bool IsReadOnly { get; }
  10. public string LuaMemberName { get; }
  11. public PropertyMetadata(ISymbol symbol, SymbolReferences references)
  12. {
  13. Symbol = symbol;
  14. IsStatic = symbol.IsStatic;
  15. if (symbol is IFieldSymbol field)
  16. {
  17. Type = field.Type;
  18. TypeFullName = field.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
  19. IsReadOnly = field.IsReadOnly;
  20. }
  21. else if (symbol is IPropertySymbol property)
  22. {
  23. Type = property.Type;
  24. TypeFullName = property.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
  25. IsReadOnly = property.SetMethod == null;
  26. }
  27. else
  28. {
  29. Type = default!;
  30. TypeFullName = "";
  31. }
  32. LuaMemberName = symbol.Name;
  33. var memberAttribute = symbol.GetAttribute(references.LuaMemberAttribute);
  34. if (memberAttribute != null)
  35. {
  36. if (memberAttribute.ConstructorArguments.Length > 0)
  37. {
  38. var value = memberAttribute.ConstructorArguments[0].Value;
  39. if (value is string str)
  40. {
  41. LuaMemberName = str;
  42. }
  43. }
  44. }
  45. }
  46. }