C# Reflection - Get field values from a simple class - c#

I have a class:
class A {
public string a = "A-val" , b = "B-val";
}
I want to print the object members by reflection
//Object here is necessary.
Object data = new A();
FieldInfo[] fields = data.GetType().GetFields();
String str = "";
foreach(FieldInfo f in fields){
str += f.Name + " = " + f.GetValue(data) + "\r\n";
}
Here is the desired result:
a = A-val
b = B-val
Unfortunately this did not work. Please help, thanks.

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...)

Remember when you write fields like :
public string VarName{ get; set;}
Then actually you have this code(this is what reflection see) :
private string _varName;
public string get_VarName(){
....
}
public void set_VarName(strig value){
....
}

As #Stanislav say, you must keep in mind the backing fields generated by the compiler for properties. If you want to exclude these fields you can use the following code:
FieldInfo[] fields = data.GetType()
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(f => f.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
.ToArray();

Related

Generic C# - how to use reflection to assign properties to generic class

I would like to assign values to various classes. How I would achieve the code below would work?
T row = new T();
row[propertyName1] = "value 1";
row[propertyName2] = "value 2";
row[propertyName3] = "value 3";
I have various classes. I have property names for each class with values.
What I am trying to do is to assign all table values from database to classes Lists. All classes in C# are exact match to tables in SQL. Entitiy framework do this. I would like to build the same functionality as EF do.
You can use such thing using reflexion:
using System.Reflection;
static void Test()
{
var list = new List<int>();
list.SetProperty("Capacity", 10);
}
static BindingFlags DefaultBindingFlags
= BindingFlags.Public
| BindingFlags.Static
| BindingFlags.Instance;
static void SetProperty<T>(this T instance, string name, object value)
//where T : ...
{
PropertyInfo p = instance.GetType().GetProperty("Capacity", DefaultBindingFlags);
if ( p != null )
p.SetValue(instance, value, null);
else
throw new Exception($"Property {name} doesn't exist.");
}

Activator.CreateInstance with string

I'm trying to populate a generic List< T > from another List< U > where the field names match, something like the untested pseudocode below. Where I'm having problems is when T is a string, for instance, which has no parameterless constructor. I've tried adding a string directly to the result object, but this gives me the obvious error -- that a string is not of Type T. Any ideas of how to solve this issue? Thanks for any pointers.
public static List<T> GetObjectList<T, U>(List<U> givenObjects)
{
var result = new List<T>();
//Get the two object types so we can compare them.
Type returnType = typeof(T);
PropertyInfo[] classFieldsOfReturnType = returnType.GetProperties(
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.NonPublic |
BindingFlags.Public);
Type givenType = typeof(U);
PropertyInfo[] classFieldsOfGivenType = givenType.GetProperties(
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.NonPublic |
BindingFlags.Public);
//Go through each object to extract values
foreach (var givenObject in givenObjects)
{
foreach (var field in classFieldsOfReturnType)
{
//Find where names match
var givenTypeField = classFieldsOfGivenType.Where(w => w.Name == field.Name).FirstOrDefault();
if (givenTypeField != null)
{
//Set the value of the given object to the return object
var instance = Activator.CreateInstance<T>();
var value = field.GetValue(givenObject);
PropertyInfo pi = returnType.GetProperty(field.Name);
pi.SetValue(instance, value);
result.Add(instance);
}
}
}
return result;
}
If T is string and you have already created custom code to convert your givenObject to a string, you just need to do an intermediate cast to object to add it to a List<T>:
public static List<T> GetObjectList2<T, U>(List<U> givenObjects) where T : class
{
var result = new List<T>();
if (typeof(T) == typeof(string))
{
foreach (var givenObject in givenObjects)
{
var instance = givenObject.ToString(); // Your custom conversion to string.
result.Add((T)(object)instance);
}
}
else
{
// Proceed as before
}
return result;
}
Incidentally, you are adding an instance of T to result for every property of T that matches a property name in U and for every item in givenObjects. I.e. if givenObjects is a list of length 1 and T is a class with 10 matching properties, result could end up with 10 entries. This looks wrong. Also, you need to watch out for indexed properties.
As an alternative to this approach, consider using Automapper, or serializing your List<U> to JSON with Json.NET then deserializing as a List<T>.

Referencing a field with a variable

I was wondering if it was possible to referance a class field via variable. like so:
int variable = 0;
while (variable > 3)
{
class._fieldvariable = something;
i++
}
where if I have fields: _field1, _field2, _field3 I can iterate through them all.
The main reason for doing this is I have an sql query that will append multiple records and I'd rather not have to do all the parameters multiple times but rather something like this:
while (i < 4)
}
command.Parameters.AddWithValue("#Alpha1", _alphai01.ToString());
i++
}
to let me set parameters 3 times with _alpha101, _alpha201 and _alpha301 are used for three different queries.
thanks!
Associate properties to the fields, all of them with get/set access.
If we're really talking about three fields, a more or less clean way to do so is by using a function GetField(int index) which would return the corresponding property. Then your code can be
class.GetField(i) = something;
An array is better in the more general case (for example, if the number of fields is expected to change).
I would prefer not to use reflection for such a simple purpose.
You can use invokeMember to call a certain setter of a property:
Object obj; //your instance;
obj.GetType().InvokeMember("Alpha1",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder, obj, "ValueForAlpha1");
that's equal to
obj.Alpha1 = "ValueForAlpha1";
http://msdn.microsoft.com/en-US/library/vstudio/66btctbe.aspx
You could use reflection like this
if you have a class A
public class A
{
int field1, field2, field3;
}
you could set these fields like this
A obj = new A();
for (int i = 1; i < 4; i++)
{
FieldInfo field = obj.GetType().GetField(String.Format("field{0}", i), BindingFlags.NonPublic | BindingFlags.Instance);
if (null != field)
{
field.SetValue(obj, i);
}
}

Reflection class to get all properties of any object

I need to make a function that get all the properies of an object (including an children objects) This is for my error logging feature.
Right now my code always returns 0 properties.
Please let me know what I'm doing wrong, thanks!
public static string GetAllProperiesOfObject(object thisObject)
{
string result = string.Empty;
try
{
// get all public static properties of MyClass type
PropertyInfo[] propertyInfos;
propertyInfos = thisObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Static);//By default, it will return only public properties.
// sort properties by name
Array.Sort(propertyInfos,
(propertyInfo1, propertyInfo2) => propertyInfo1.Name.CompareTo(propertyInfo2.Name));
// write property names
StringBuilder sb = new StringBuilder();
sb.Append("<hr />");
foreach (PropertyInfo propertyInfo in propertyInfos)
{
sb.AppendFormat("Name: {0} | Value: {1} <br>", propertyInfo.Name, "Get Value");
}
sb.Append("<hr />");
result = sb.ToString();
}
catch (Exception exception)
{
// to do log it
}
return result;
}
here's what the object looks like:
If you want all of the properties, try:
propertyInfos = thisObject.GetType().GetProperties(
BindingFlags.Public | BindingFlags.NonPublic // Get public and non-public
| BindingFlags.Static | BindingFlags.Instance // Get instance + static
| BindingFlags.FlattenHierarchy); // Search up the hierarchy
For details, see BindingFlags.
The problem with your code is the PayPal Response types are members, NOT properties. Try:
MemberInfo[] memberInfos =
thisObject.GetMembers(BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance);
Your propertyInfos array is returning 0 length for one of my classes. Changing the line to be
propertyInfos = thisObject.GetType().GetProperties();
Results in it being populated. Therefore, this line of code is your problem. It appears if you add the flag
BindingFlags.Instance
to your parameters it will return the same properties as the parameterless call. Does adding this parameter to your list fix the problem?
EDIT: Just saw your edit. Based on the code you posted, it didn't work for me either. Adding the BindingFlags.Instance made it return properties for me. I'd suggest posting the exact code you are having trouble with as your screenshot shows different code.

GetValue on static field inside nested classes

I have the following class declared. I need to retreive the class structure and the static values without instanciate it.
public class MyClass()
{
public static string field = "Value";
public class nestedClass()
{
public static string nestedField = "NestedValue";
}
}
I've successfuly used GetFields and GetNestedType to recover the class structure and GetValue(null) works fine on field, but not on nestedField.
Let me sample:
var fi = typeof(MyClass).GetField("field", BindingFlags.Public | BindingFlags.Static);
var nt = typeof(MyClass).GetNestedType("nestedClass", BindingFlags.Public);
var nfi = nt.GetField("nestedField", BindingFlags.Public | BindingFlags.Static);
// All the above references are detected correctly
var value = fi.GetValue(null); // until here everything works fine. value == "Value"
var nestedValue = nfi.GetValue(null); // this one does not work!!
Anyone knows why the last line does not work and how to work around?
Thanks.
Well it all seems to work fine to me and after the last line I get the "NestedValue" string. Tried on .net frameworks 3.5 , 4 and 4.5, everything works (VS2012 Pro). ReSharper states though, that fi, and nfi might be a possible null reference.
public class MyClass()
{
Don't put "()" at the end of your class, that code is still experimental, and it's redundant unless you actually want to use the experimental code, then you'd have to provide some arguments to the constructor.

Categories