以下の回答に従って、派生クラスで特定のパラメーター化されたコンストラクターの使用を強制しようとしています。
上記の回答で提供されている例を使用すると、コードのコンパイルは期待どおりに失敗します。コードを変更して私のものに似せた後でも、まだ失敗します。私の実際のコードは問題なくコンパイルされます。それがなぜなのか、私はここで途方に暮れています。
提供された回答から変更された例を次に示します(期待どおりにコンパイルされません)。
public interface IInterface
{
void doSomething();
}
public interface IIInterface : IInterface
{
void doSomethingMore();
}
public abstract class BaseClass : IIInterface
{
public BaseClass(string value)
{
doSomethingMore();
}
public void doSomethingMore()
{
}
public void doSomething()
{
}
}
public sealed class DerivedClass : BaseClass
{
public DerivedClass(int value)
{
}
public DerivedClass(int value, string value2)
: this(value)
{
}
}
今、問題なくコンパイルされる私のコード:
public interface IMethod
{
Url GetMethod { get; }
void SetMethod(Url method);
}
public interface IParameterizedMethod : IMethod
{
ReadOnlyCollection<Parameter> Parameters { get; }
void SetParameters(params Parameter[] parameters);
}
public abstract class ParameterizedMethod : IParameterizedMethod
{
public ParameterizedMethod(params Parameter[] parameters)
{
SetParameters(parameters);
}
private Url _method;
public Url GetMethod
{
get
{
return _method;
}
}
public void SetMethod(Url method)
{
return _method;
}
public ReadOnlyCollection<Parameter> Parameters
{
get
{
return new ReadOnlyCollection<Parameter>(_parameters);
}
}
private IList<Parameter> _parameters;
public void SetParameters(params Parameter[] parameters)
{
}
}
public sealed class AddPackageMethod : ParameterizedMethod
{
public AddPackageMethod(IList<Url> links)
{
}
public AddPackageMethod(IList<Url> links, string relativeDestinationPath)
: this(links)
{
}
private void addDownloadPathParameter(string relativeDestinationPath)
{
}
private string generatePackageName(string destination)
{
return null;
}
private string trimDestination(string destination)
{
return null;
}
}
できるだけ簡潔にするために、一部のメソッドの実装を削除しました。補足として、私の実際のコードには一部の領域が欠けている可能性があります。これらのパーツは WIP と考えてください。
更新 1/解決策:
以下のsstanの回答によると、キーワード「params」を使用することの意味を指摘しているように、ここではコードの修正された一節であり、意図したとおりに動作します(コンパイルに失敗します):
public abstract class ParameterizedMethod : IParameterizedMethod
{
public ParameterizedMethod(Parameter[] parameters) // **'params' removed**
{
SetParameters(parameters);
}
// original implementation above
}