Having those 2 classes:
class A
{
public A()
{
strA1 = "A1";
strA2 = "A2";
strA3 = "A3";
}
public string strA1 { get; set; }
public string strA2 { get; set; }
public string strA3 { get; set; }
}
class B
{
public B()
{
strB1 = "B1";
strB2 = "B2";
}
public string strB1 { get; set; }
public string strB2 { get; set; }
}
I am trying to find a way to have a single method (probably override toString()) that will generates information based on the number of properties in these classes.
For example the result would be:
for Class A: "{\""A1\"",\""A2\"",\""A3\""}"; // {"A1","A2","A3"}
for Class B: "{\""B1\"",\""B2\""}"; // {"B1","B2"}
How Can be done in a generic way without writing specific code in each class?
Probably a Base class is the starting … Please advise
You can get type info and public properties value with Reflection. Here is an extension method:
Usage:
Output: