Grab reference to static members or functions that has custom attribute attached

I am trying to create a custom attribute that I can attach to either variables or functions, such that I could grab all references somewhere else in code.


[Attribute1]                        //custom attribute
public static int speed = 1;        //dummy variable
[Attribute1]                        //custom attribute
public static int acceleration = 1; //dummy variable
[Attribute2]                        //custom attribute
public static int count = 1;        //dummy variable

public static void DummyFunction() {...}

and from somewhere else, be able to do something along the lines of

var allAttributeReferences = GetAllAttributes("Attribute1");
allAttributeReferences[0] = 10;

var allAttributeReferences2 = GetAllAttributes("Attribute2");
foreach (var reference in allAttributeReferences2)
    reference = 0;

var allAttributeReferences3 = GetAllAttributes("Attribute3")[0];
allAttributeReferences3(); //Assume it is a function, and invoke function.

Is this even remotely possible? Could someone point me in the right direction?

You may use reflection to get fields and methods having the attribute. But you won’t get a reference to them, only setters / getters for the fields and invokers for the methods.

Look for .Net/C# reflection, FieldInfo, MethodInfo…