I am making a simulator for simulating interactions between planets of the solar system and asteroids etc. Seeing a lot of the code I am writing is very similar to the code I already wrote for an earlier project where I simulated oil spills, I want to use generics to reuse my old code. I have several classes that form the simulator, and one class that inhabits the simulation (celestial objects in this case), which is given as a generic type parameter. The simulator needs to know how many frames it has to store for recursive formulas, which is determined by the generic parameter.
public class Sim<T> where T : IEntity
{
readonly int RecursionDepth; //need to get the value from the class passed as T
//some more code
}
public interface IEntity
{
//cant require a const here, it would be a default implementation instead
//don't want a int property with get; here as there are no guarantees about the immutability
//and different instances may have different values.
//some code
}
It would make sense to have this value be a constant or a static readonly field of the class used in the simulation. However, neither of these two options can be required using an interface. Sure, I can add a property in the interface definition, but as far as I am aware I cannot require it being readonly, and the same for each instance of the class. I really do not want to have to resort to the situation where it is just a random integer passed to the system, and I have to trust it is going to work out.
Are there any requirements I can set to at least have reasonable confidence in the immutability of the parameter and its egality across all instances of the class?