I am working on a legacy C# application that includes many business entity classes with a 5 common properties same name same type (string and integer).
I need to implement some business process logic on the entity classes based on the 5 common properties.
class A
{
public string CommonProperty1 {get;set;}
public int CommonProperty2 {get;set;}
public string CommonProperty3 {get;set;}
public string CommonProperty4 {get;set;}
public string CommonProperty5 {get;set;}
}
class B
{
public string CommonProperty1 {get;set;}
public int CommonProperty2 {get;set;}
public string CommonProperty3 {get;set;}
public string CommonProperty4 {get;set;}
public string CommonProperty5 {get;set;}
}
class C
{
public string CommonProperty1 {get;set;}
public int CommonProperty2 {get;set;}
public string CommonProperty3 {get;set;}
public string CommonProperty4 {get;set;}
public string CommonProperty5 {get;set;}
}
// there more of such business classes
public static BusinessHelpr
{
public static DoSomethingOnClassAorBorC(object theRefrence)
{
theRefrence.CommonProperty4 = "result of some complex calculation";
theRefrence.CommonProperty2 = 56; // result of some complex calculation;
theRefrence.CommonProperty5 = "result of some complex calculation";
}
}
If this was a greenfield application, I would inherit from a base class, that includes the 5 properties, and nicely implement the required logic
However, there is a decision that we do not do any refactoring or changing the business entities. They cannot be touched.
As such, In my helper class I need to find a way to get a reference to an object type, and access to its properties by the name of them in a string. An obvious option here is reflection. That means I get a type, use reflection to access its properties by name in a string. However, I learned that the use of reflection in this scenario imposes performance penalty and it is not a good practice.
Please note that I simplified the actual scenario to focus on the main point. so creating a method below won't work:
static DoSomethingOnClassAorBorC(string CommonProperty1, int CommonProperty2, string CommonProperty3,string CommonProperty4, string CommonProperty5)
What are my other options, other than reflection?
Aucun commentaire:
Enregistrer un commentaire