Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 }
- );
- }
- public static class InstanceMap
- {
- private static readonly Dictionary<Type,object> instances =
- new Dictionary<Type,object>();
- public static void AddInstance(object instance)
- {
- instances[instance.GetType()] = instance;
- }
- public static T GetInstance<T>() { return (T) instances[typeof(T)]; }
- }
- public interface INonCompoundedInterestRate
- {
- INonCompoundedInterestRate ImpliedRate(double factor,
- double time,
- DayCounter dayCounter);
- }
- public class MyNonCompoundedInterestRate: INonCompoundedInterestRate
- {
- public INonCompoundedInterestRate ImpliedRate(double factor,
- double time,
- DayCounter dayCounter) { /* do smth*/ }
- static MyNonCompoundedInterestRate()
- {
- InstanceMap.AddInstance(new MyNonCompoundedInterestRate());
- }
- }
- public abstract class InterestRate {
- public static T ImpliedRate<T>(
- double factor,
- double time,
- DayCounter dayCounter
- ) where T : INonCompoundedInterestRate
- {
- return InstanceMap.GetInstance<T>().
- ImpliedRate(factor, time, dayCounter);
- }
- // ...
- }
- public static class InterestRateFactories
- {
- static InterestRateFactories()
- {
- _factories = new List<IInterestRateFactory>();
- // register default factories, although you can also register them elsewhere, like in your ioc setup
- }
- private static readonly List<IInterestRateFactory> _factories;
- public static void RegisterFactory(IInterestRateFactory factory)
- {
- _factories.Add(factory);
- }
- public static T ImpliedRate<T>(double factor, double time, DayCounter dayCounter)
- where T : NonCompoundedInterestRate
- {
- var factory = _factories.FirstOrDefault(x => x.CanCreate(typeof(T), false));
- if (factory == null)
- {
- throw new NotSupportedException("Cannot create a non-compounded implied interest rate of type " + typeof(T).Name);
- }
- return (T)factory.Create(factor, time, dayCounter);
- }
- public static T ImpliedRate<T>(double factor, double time, DayCounter dayCounter, Frequency frequency)
- where T : CompoundedInterestRate
- {
- var factory = _factories.FirstOrDefault(x => x.CanCreate(typeof(T), false));
- if (factory == null)
- {
- throw new NotSupportedException("Cannot create a compounded implied interest rate of type " + typeof(T).Name);
- }
- return (T)factory.Create(factor, time, dayCounter, frequency);
- }
- }
- public interface IInterestRateFactory
- {
- bool CanCreate(Type nonCompoundedInterestRateType, bool compounded);
- NonCompoundedInterestRate Create(double factor, double time, DayCounter dayCounter);
- CompoundInterestRate Create(double factor, double time, DayCounter dayCounter, Frequency frequency);
- }
Add Comment
Please, Sign In to add comment