samedi 6 juin 2020

Get ref to field value in C#

I need to get a reference to a field value in C# but I cant find any way to get an actual reference to the field that can be passed as a parameter. Any help will be greatly appreciated.

Example:

public class SomeClass 
{
    public int value = 0;
}

public class SomeOtherClass
{
    static void Main(string[] args)
    {
        SomeClass x = new SomeClass();
        Console.WriteLine(x.value);       //Outputs "0"

        foreach (FieldInfo field in x.GetType().GetFields())
        {
            Log(ref field.value)          //Outputs "10"
        }          //       ^
    }              //Not a valid member

    static void Log(ref int i)
    {
        i = 10;
        Console.WriteLine(i);
    }

}




Aucun commentaire:

Enregistrer un commentaire