I have been trying to find an elegant way to avoid repeating code in all of my derived classes.
(我一直在尝试寻找一种优雅的方法来避免在所有派生类中重复代码。)
At this point, I am unsure as to the best way to proceed. (在这一点上,我不确定最好的处理方法。)
I'd like to write a single method in the base class that will instantiate and use any of its derived classes without having to edit the method when I write new derived classes.
(我想在基类中编写一个方法,该方法将实例化和使用其任何派生类,而在编写新的派生类时不必编辑该方法。)
I have tried learning/using a generic method but started to think I might be heading down a dead end for this application.
(我尝试学习/使用通用方法,但开始认为我可能会为此应用程序陷入困境。)
I understand that using reflection can be expensive, and since this method is meant to handle hundreds or even thousands of Elements, it seemed like a bad idea. (我知道使用反射可能会很昂贵,并且由于此方法旨在处理成百上千个Element,因此似乎是个坏主意。)
Now I'm thinking of trying to pass in the class itself as an argument somehow... maybe.
(现在我正在考虑尝试以某种方式将类本身作为参数传递……也许。)
That doesn't seem quite right to me either. (对我来说,这也不是很正确。)
I'm willing to do the research, but would love any help pointing me in the right direction.
(我愿意进行研究,但是很乐意为我指明正确的方向。)
Here is an abridged version of what I have...
(这是我所拥有的简要版本...)
Base Class:
(基类:)
public abstract class Element
{
public string ElementName { get; }
public List<string> BadParameters { get; set; } = new List<string>();
//Constructor
public Element(string name)
{
ElementName = name;
}
//The method in question---
public static List<string> GetBadParameters(//derived class to instantiate)
{
var elem = new //derived class();
return elem.BadParameters;
}
}
Derived Class 1:
(派生类1:)
public class Wall : Element
{
public double Length { get; set; }
public bool LoadBearing { get; set; }
//Constructor
public Wall(string name): base(name)
{
SetBadParameters();
}
public void SetBadParameters()
{
BadParameters = //A wall specific way of setting bad parameters
}
}
Derived Class 2:
(派生类2:)
public class Floor : Element
{
public double Area { get; set; }
public double Slope { get; set; }
//Constructor
public Floor(string name): base(name)
{
SetBadParameters();
}
public void SetBadParameters()
{
BadParameters = //A floor specific way of setting bad parameters
}
}
Implementation:
(实现方式:)
public class Implementation
{
public List<string> GetAllBadElementParameters()
{
List<string> output = new List<string>;
List<string> badWalls = GetBadParameters(//Wall class)
List<string> badFloors = GetBadParameters(//Floor class)
output = output.AddRange(badWalls).AddRange(badFloors);
return output;
}
}
EDIT - To clarify:
(编辑-要澄清:)
The actual content of
(实际内容)
public List<string> BadParameters
does not matter.
(没关系。)
Bad parameters, how and why they are bad, are inconsequential. (不好的参数,如何以及为什么不好是无关紧要的。)
What I'm trying to accomplish is avoid having the method "GetBadParameters()" defined in the derived class, since this method will be the exact same for all derived classes.
(我要完成的工作是避免在派生类中定义方法“ GetBadParameters()”,因为该方法对于所有派生类都是完全相同的。)
It is only the populating of the "BadParameter" base class property that changes from one derived class to another.
(只是从一个派生类到另一个派生类的“ BadParameter”基类属性的填充。)
EDIT 2 - My attempt at a generic method in the base class:
(编辑2-我在基类中的通用方法的尝试:)
I know this won't work, but it may convey what I'd like to have happen.
(我知道这行不通,但可以传达我想发生的事情。)
public static List<string> GetAllBadParameters<T>(List<string> names) where T : ANY DERIVED CLASS, new()
{
List<string> output = new List<string>();
foreach (string name in names)
{
var elem = new T(name);
foreach (string badParameter in elem.BadParameters)
{
output.Add(badParameter);
}
}
return output;
}
ask by Leetheus translate from so