One of our unit tests is to populate properties within our business objects with random data.
These properties are of different intrinsic types and therefore we would like to use the power of generics to return data of the type you pass in. Something along the lines of:
public static T GetData<T>()
How would you go about approaching this? Would a low level interface work? (IConvertible)
It depends on what data you want to randomize, because the way or the algorithm you want to use is totally different depending on the type.
For example:
So this obviously makes the use of generics a nice semplification on the user’s end, but it adds no value to the way you write the class. You could use a switch clause or a dictionary (like Jon Skeet suggests):
Then you would use the class as you expect: