在派生构造函数中的某些代码块之后,在派生类中调用基类构造函数

use*_*718 7 .net c# constructor c#-5.0

public class bar
{
   public bar(list<int> id, String x, int size, byte[] bytes)
   {
     ...
   }
}

public class Foo: Bar
{
    public Foo(list<int> id, String x, someEnumType y):
     base(id, x, sizeof(someEnumType), y)
    {
        //some functionality
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您在上面的代码中看到的,我需要在调用基类构造函数之前将someEnumType转换为字节数组类型.有办法吗?就像是:

public class Foo: Bar
{
    public Foo(list<int> id, String x, someEnumType y)
    {
        //someEnumType to byte array
        base(...)

    }
} 
Run Code Online (Sandbox Code Playgroud)

puk*_*uko 4

只需在派生类中创建一个方法并调用它......

public class bar
{
   public bar(list<int> id, String x, int size, byte[] bytes)
   {
     ...
   }
}

public class Foo: Bar
{
    public Foo(list<int> id, String x, someEnumType y):
     base(id, x, sizeof(someEnumType), Convert(y))
    {
        //some functionality
    }

    public static byte[] Convert(SomeEnumType type)
    {
        // do convert
    }
}
Run Code Online (Sandbox Code Playgroud)