C# Override with different parameters?

后端 未结 5 1522
天命终不由人
天命终不由人 2020-12-11 16:29

Here is an example of what I am looking to do.

public class A
{
    public virtual void DoSomething(string a)
    {
      // perform something
    }
}
public         


        
5条回答
  •  青春惊慌失措
    2020-12-11 16:57

    There is a way. It requires some code though. You create a master class fex. 'MachineParams'. Then create subclasses like 'MachineParamsGeneral', 'MachineParamsDetail'.

    Your function can now be like:

    public override void Setup(MachineParams p)
    {
    // since you know what subclass this is inside, you can simple cast
    MachineParamsGeneral g = (MachineParamsGeneral)p;
    float paramExample = p.paramExample;
    }
    

    You need to consider if this solution is "handy" enough for your scale.

    A second solution:

    public override void Setup(params object[] p ..)
    {
    // you need to know the arrangement of values inside the object array
    float x = (float)p[0]; // not strictly speaking type-safe
    }
    

提交回复
热议问题