Why does my custom Attribute not listed in reflectection

Godot Version

Godot 4.4.1 Mono

Question

I just adapted mono version as i want to use c# for some metaprogramming.

So i created one my customized Attribute:

using System;
using Metalama.Framework.Aspects;

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
[RunTime]
public class IdentifierAttribute : Attribute
{

    public string BlanketField { get; set; }

}

And try to use it in my class:

using Godot;
using Godot.Collections;
using System;

[GlobalClass]
public partial class Unit : SerializableCS
{

    const string Source = "data/units.csv";

    [Identifier]
    public int Id { get; set; }

    private string Name { get; set; }

    private Vector2I Coordinate { get; set; }
}

And in another class, i tried to fetch the Attribute from property Id:

        var members = toType.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        foreach (var item in members)
        {
              GD.Print(item.Name);
               GD.Print(item.GetCustomAttributes());
         }

But all the members, print empty Attributes. I tried to list Properties, result are same.
Also tried add some Godot custom Attribute like [Export]. Also no luck, which is understanddable as those are work in editor process only.

Output be like:
Id
System.Attribute
Name
System.Attribute
AttributeCorrection
System.Attribute
MoveRange
System.Attribute
k__BackingField
System.Attribute
k__BackingField
System.Attribute

I just want some of my own attribute to be found in Runtime, can someone help?
[Runtime] Attribute from Metalama is some attemptence but not working also. tried RuntimeOrCompileTime as well.

I believe this feature is not implemented yet for C# but I could be wrong.

The GetCustomAttributes method returns an IEnumerable<T> and the default object.ToString implementation only prints the type name.

To print the elements in the enumerable, you’ll have to do something like this:

var members = toType.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var item in members)
{
    GD.Print(item.Name);
    foreach (var attribute in item.GetCustomAttributes())
    {
        GD.Print($" - {attribute}");
    }
}

@tibaverus C# attributes are a language feature, so there’s nothing that needs to be implemented on the Godot side for them to work.

1 Like

OMG YOU ARE GENESIS

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.