C# Reflection - Get Field Values from a Simple Class

C# Reflection - Get field values from a simple class

Once fixed to get rid of the errors (lacking a semi-colon and a bad variable name), the code you've posted does work - I've just tried it and it showed the names and values with no problems.

My guess is that in reality, you're trying to use fields which aren't public. This code:

FieldInfo[] fields = data.GetType().GetFields();

... will only get public fields. You would normally need to specify that you also want non-public fields:

FieldInfo[] fields = data.GetType().GetFields(BindingFlags.Public | 
BindingFlags.NonPublic |
BindingFlags.Instance);

(I hope you don't really have public fields, after all...)

Get fields and values from an object by reflection in C#

Yeah I think this will work:

Dictionary<string, string> listField =
membership.GetType()
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance) // <-- specify that you want instance fields
.ToDictionary(f => f.Name,
f => (string)f.GetValue(membership)); // <-- IMPORTANT,
// you need to specify an instance to get a value from a non-static field

The above code will only work for instance fields without modification

How do I use reflection to get a list of values of an object's fields that match a particular type?

I don't think that using reflection would be better than adjusting your property each time you add a field, but there you go:

public class Person
{
private int _age;
public int _phoneNumber;
// protected int _futureInt;
Dictionary<string, string> _readingList = new Dictionary<string, string>();

public Person(int age){
_age = age;
}

public bool personProperty
{
get
{
List<bool> resultList = new List<bool>();
var intFields = this.GetType().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public)
.Where(f => f.FieldType == typeof(int))
.Select(f => f.GetValue(this)).Cast<int>();
foreach (int personFieldValue in intFields)
{
bool result = personMethod(personFieldValue);
resultList.Add(result);
}
// Do stuff with `resultList` that'll initialize personPropertyReturnValue;
bool personPropertyReturnValue = resultList.All(b => b);
return personPropertyReturnValue;
}
}

private bool personMethod(int arg)
{
return (arg > 0);
}
}

Test:

var person1 = new Person(0);
Console.WriteLine(person1.personProperty); // False

var person2 = new Person(1);
Console.WriteLine(person2.personProperty); // False

var person3 = new Person(1) { _phoneNumber = 1 };
Console.WriteLine(person3.personProperty); // True

Get property value from string using reflection

 public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}

Of course, you will want to add validation and whatnot, but that is the gist of it.

C# - Get class field property value

You must pass the instance from which you want to get the value.

First you have to get memberAField.GetValue(classA) so you have the value of the field, equivalent to classA.classAA. Then call get value using this result classAProperty.GetValue(memberAField.GetValue(classA)), equivalent to classA.classAA.PeropertyA.

        Console.WriteLine(" to value: " + classAProperty.GetValue(memberAField.GetValue(classA)));

Full code :

classA classA = new classA();
classA.classAA.propertyA = "A";
classA.classAB.propertyB = "B";

foreach (FieldInfo memberAField in classA.GetType().GetFields()) {
Console.WriteLine(memberAField.Name + " " + memberAField.MemberType + " " + memberAField.FieldType);
object memberAValue = memberAField.GetValue(classA);
foreach (PropertyInfo classAProperty in memberAField.FieldType.GetProperties()) {
Console.WriteLine("name: " + classAProperty.Name);
if (memberAValue == null)
Console.WriteLine(" no value available");
else
Console.WriteLine(" to value: " + classAProperty.GetValue(memberAValue));
}
}

How to get the value of a field c#

The GetField() method requires an instance argument:

var val = field.GetValue(Pages);

Here's a self-contained example (also a link to .NET Fiddle):

using System;
using System.Reflection;

public class Program
{
public static void Main()
{
var item = new SomeType();
item.FieldA = "hi";
item.FieldB = "bye";

foreach(var field in item.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)) {
var val = field.GetValue(item);
Console.WriteLine("In the 'item' instance, field " + field.Name + " has value " + val);
}
}
}

public class SomeType {
public string FieldA;
public string FieldB;
}

C# Reflection - Get value of unknown field

Type.GetField(string) only returns public fields. I suspect you want:

FieldInfo fieldInfo = editorGUIType.GetField(
"s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static);


Related Topics



Leave a reply



Submit