これは汚いことであり、私はそれを行うことに汚いと感じています:
public abstract class InterestRate {
// irrelevant details
public static T ImpliedRate<T>(
double factor,
double time,
DayCounter dayCounter
) where T : NonCompoundedInterestRate {
MethodInfo methodInfo = typeof(T).GetMethod(
"ImpliedRate",
BindingFlags.Static);
return (T)methodInfo.Invoke(
null,
new object[] { factor, time, dayCounter }
);
}
public static T ImpliedRate<T>(
double factor,
double time,
DayCounter dayCounter,
Frequency frequency
) where T : CompoundedInterestRate {
MethodInfo methodInfo = typeof(T).GetMethod(
"ImpliedRate",
BindingFlags.Static);
return (T)methodInfo.Invoke(
null,
new object[] { factor, time, dayCounter, frequency }
);
}
ここにクラスNonCompoundedInterestRate
(抽象)がありCompoundedInterestRate
、抽象クラスから派生していInterestRate
ます。上記のリフレクションが機能するための適切なシグネチャで名前がNonCompoundedInterestRate
付けられた静的メソッドを持ついくつかの具体的な実装があります。ImpliedRate
リフレクションを使用して、派生クラスにあることが保証されていない静的メソッドを呼び出すと、悪臭がします。これを処理するより良い方法はありますか?