2
How to change the value of the class property in the list?
My variable classPropValueList
always comes void.
I’m trying to make a Reflection of every class it inherits from Coisable
, to change its string-like properties.
It is possible to find the following properties in a class that inherits from Coisable
:
- A string type property (which must be changed)
- A property of the type of some other class that also inherits from
Coisable
(this class must have its string-like properties changed, so here is the need for recursive use). - A collection type property of some other class that also inherits from
Coisable
, and for each of the collection’s items, their properties of typestring
should be amended.
I then managed, through the code below, to solve scenario 1 and 2, but I had problems with the 3rd scenario. To thresh the code, I saw that in the case of example, it is possible to enter the third condition, but when trying to get the list, it always comes null.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var coisa1 = new MyClass3
{
MyProperty1 = "Valor Original 1",
MyProperty2 = new MyClass1
{
MyProperty = "Valor Original 2"
},
MyProperty3 = new List<MyClass2>
{
new MyClass2
{
MyProperty = "Valor Original 3"
}
}
};
Console.WriteLine("--- Valores Originais ---");
Console.WriteLine(coisa1.MyProperty1);
Console.WriteLine(coisa1.MyProperty2.MyProperty);
Console.WriteLine(coisa1.MyProperty3.First().MyProperty);
Console.WriteLine();
coisa1.Coise();
Console.WriteLine("--- Novos Valores ---");
Console.WriteLine(coisa1.MyProperty1);
Console.WriteLine(coisa1.MyProperty2.MyProperty);
Console.WriteLine(coisa1.MyProperty3.First().MyProperty);
}
}
// #region Minhas Classes
public class Coisable
{
}
public class MyClass1 : Coisable
{
public string MyProperty { get; set; }
}
public class MyClass2 : Coisable
{
public string MyProperty { get; set; }
}
public class MyClass3 : Coisable
{
public string MyProperty1 { get; set; }
public MyClass1 MyProperty2 { get; set; }
public List<MyClass2> MyProperty3 { get; set; }
}
// #endregion
// #region Reflection
public static class CoisableExt
{
public static void Coise(this Coisable coisableClass) {
foreach (var propertyInfo in coisableClass.GetType().GetProperties())
{
if(typeof(string).IsAssignableFrom(propertyInfo.PropertyType))
{
var propValue = propertyInfo.GetValue(coisableClass, null).ToString();
propValue = "Novo Valor";
propertyInfo.SetValue(coisableClass, propValue, null);
}
else if(typeof(Coisable).IsAssignableFrom(propertyInfo.PropertyType))
{
var classPropValue = propertyInfo.GetValue(coisableClass, null) as Coisable;
classPropValue.Coise();
}
else if (typeof(IEnumerable<Coisable>).IsAssignableFrom(propertyInfo.PropertyType))
{
var classPropValueList = propertyInfo.GetValue(coisableClass, null) as List<Coisable>;
if(classPropValueList != null && classPropValueList.Any())
{
classPropValueList.ForEach(classPropValueItem =>
{
classPropValueItem.Coise();
});
}
}
}
}
}
// #endregion
Can you explain a little better what you want? In the current way you need to read all the code to try to understand the problem
– Jéf Bueno
I edited the question, adding some more details...
– Jedaias Rodrigues
By the way, two notes: 1. I liked the class name (
Coisable
); 2. Very good create a [mcve], great idea.– Jéf Bueno
I honestly don’t know if it was a compliment or an ear tug, haha...
– Jedaias Rodrigues
They were two compliments =D It’s good to have a code that you only need to copy and paste to run and understand the problems.
– Jéf Bueno