0

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);
    }

}
Beckam White
  • 143
  • 10
  • I've used field.GetValue(null).ToString() Why would you need the reference value tho? You're basically just handling it as a constant to the Log method. – mrbitzilla Jun 07 '20 at 04:04
  • 2
    You've already asked this question in the last day. [Get Reference to Field from Reflection](https://stackoverflow.com/questions/62234090/get-reference-to-field-from-reflection) Please don't ask a new question just because you didn't receive answers on the old one. I think you need to clarify your requirements a bit better -- if you can I actually might have a solution for you – pinkfloydx33 Jun 07 '20 at 10:58
  • @rmed1na this is an analogy for what im trying to achieve. I need to be able to get an active reference to the field and pass it as a ref parameter using reflection. – Beckam White Jun 07 '20 at 11:08
  • @BeckamWhite why not pass the object as a reference itself? I mean, I still see no usage or why to pass it as a reference. Reference parameters usage is to actually change the content of the object being passed as a parameter. If you need to change it, pass the entire object as a ref and change it on the working method, internally. – mrbitzilla Jun 07 '20 at 23:56

2 Answers2

0

Try the below

public class SomeClass
{
    public int value = 0;
}

public class SomeOtherClass
{
    public static void Main(string[] args)
    {
        SomeClass x = new SomeClass();
        Console.WriteLine(x.value); // Output Line 1
        foreach (FieldInfo field in x.GetType().GetFields())
        {
            Console.WriteLine(field.GetValue(x)); // Output Line 2
            var y = (int)field.GetValue(x);
            Log(ref y);
            Console.WriteLine(y); // Output Line 4
        }
    }

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

Output: 0 0 10 10

0
  class Program
    {

        public class SomeClass
        {
            public int value = 0;
        }

        public class SomeOtherClass
        {
            static void Main(string[] args)
            {
                var x = new SomeClass();
                Console.WriteLine(x.value);       //Outputs "0"
                foreach (FieldInfo field in x.GetType().GetFields())
                {
                    var value = (int)field.GetValue(x);
                    Log(ref x);
                    Console.WriteLine(x.value);
                }
            }

            static void Log(ref SomeClass x)
            {
                x.value = 10;
            }

        }

    }