C# Iterate Through Class Properties

C# Iterate through Class properties

You could possibly use Reflection to do this. As far as I understand it, you could enumerate the properties of your class and set the values. You would have to try this out and make sure you understand the order of the properties though. Refer to this MSDN Documentation for more information on this approach.

For a hint, you could possibly do something like:

Record record = new Record();

PropertyInfo[] properties = typeof(Record).GetProperties();
foreach (PropertyInfo property in properties)
{
property.SetValue(record, value);
}

Where value is the value you're wanting to write in (so from your resultItems array).

How to loop through all the properties of a class?

Use Reflection:

Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();

foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
}

for Excel - what tools/reference item must be added to gain access to BindingFlags, as there is no "System.Reflection" entry in the list

Edit: You can also specify a BindingFlags value to type.GetProperties():

BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
PropertyInfo[] properties = type.GetProperties(flags);

That will restrict the returned properties to public instance properties (excluding static properties, protected properties, etc).

You don't need to specify BindingFlags.GetProperty, you use that when calling type.InvokeMember() to get the value of a property.

How to loop through the properties of a Class?

Something like:

object obj = new object();
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (var p in properties)
{
var myVal = p.GetValue(obj);
}

Note you need to allocate the object and pass it into the PropertyInfo.

C# iterate through class properties and add to list certain types

public static List<PropertyInfo> PropertiesOfType<T>(this Type type) =>
type.GetProperties().Where(p => p.PropertyType == typeof(T)).ToList();

And you'd use it as follows:

var properties = typeof(CustomClass).PropertiesOfType<CustomAttribute>();

If what you need are the values of the properties of type T in a given instance then you could do the following:

 public static List<T> PropertyValuesOfType<T>(this object o) =>
o.GetType().GetProperties().Where(p => p.PropertyType == typeof(T)).Select(p => (T)p.GetValue(o)).ToList();

And you'd use it as:

CustomClass myInstance = ...
var porpertyValues = myInstance.GetPropertyValuesOfType<CustomAttribute>();

Note that this just gives you the idea, you need to evaluate if dealing with properties with no getters is needed.

And last but not least, if you need the values and the property names, then you can build up a List of tuples to store the information:

public static List<Tuple<string, T>> PropertiesOfType<T>(this object o) =>
o.GetType().GetProperties().Where(p => p.PropertyType == typeof(T)).Select(p => new Tuple<string, T>(p.Name, (T)p.GetValue(o))).ToList();

Iterate through all properties of a class

The answer:

var props = typeof(Animal).GetProperties().Where(prop => prop.GetSetMethod() != null);
FarmAnimal fa = new FarmAnimal();
foreach (var prop in props)
{
typeof(FarmAnimal )
.GetProperty(prop.Name,
BindingFlags.IgnoreCase |
BindingFlags.Instance |
BindingFlags.Public | BindingFlags.SetField)

.SetValue(fa,
prop.GetValue(animal));
}

Iterate through class properties using an action?

Assuming your HeaderAttribute accepts a Header as a parameter and exposes it over Header property :

    foreach (var property in properties)
{
var attr = property.GetCustomAttributes(typeof(HeaderAttribute), false).FirstOrDefault() as HeaderAttribute;
if (attr != null)
{
//Here we use the Map method overload that takes a Type and a MemberInfo

this.Map(typeof(MyClass), property).Name(attr.Header);
}
}

Loop through an object's properties and get the values for those of type DateTime

You need to use car as the first parameter:

foreach (var car in carList)
{
foreach (PropertyInfo prop in car.GetType().GetProperties())
{
var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
if (type == typeof (DateTime))
{
Console.WriteLine(prop.GetValue(car, null).ToString());
}
}
}


Related Topics



Leave a reply



Submit