mercredi 29 août 2018

List fields with a specific attribute

I'm trying to list all the fields with a certain Attribute, but still not quite understanding what kind of object GetValue() expects.

[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
class SerializedAttribute : Attribute
{

}

class Program
{
    [Serialized] public Single AFloat = 100.0f;
    [Serialized] public Single AnotherFloat = 125.5f;
    [Serialized] public Single OnceAgain = 75.0f;

    static void Main(string[] args)
    {

        foreach(FieldInfo field in typeof(Program).GetFields())
        {
            foreach(Attribute attr in field.GetCustomAttributes())
            {
                if (attr is SerializedAttribute)
                {
                    Console.WriteLine("\t" + "Variable name: " + field.Name + "\t" + "Variable value:" + field.GetValue(/*"??????????????"*/));           
                }
            }
        }

        Console.ReadKey();

    }
}

I tried few google searches but apperantly I'm not a very good at problem solving.





Aucun commentaire:

Enregistrer un commentaire