Hi,
Thank you I appreciate your input and you got a valid point, and I can explain why making IOffset public would make life easier for me if it interests you:
Consider a simple dictionary of offset addresses and offsets. Right now it has to be declared as such:
readonly IDictionary<int, object> Offsets = new Dictionary<int, object>();
(As there are no common public denominator). Could be done with dynamic as well.
Then consider the method needed to retrieve an Offset<T> value from that dictionary.
The easy implementation is if T is known (compile time):
T GetOffsetValue<T>(int address)
{
object offset;
if (Offsets.TryGetValue(address, out offset))
{
if (offset is Offset<T>)
return ((Offset<T>)offset).Value;
else
throw new InvalidOperationException(string.Format("Value of requested offset {0} does not match the expected type {1}", address, typeof(T)));
}
Offsets.Add(address, new Offset<T>(address));
return default(T);
}
But if you don't know T (as in getting an Offset from a Type), the code becomes a little messier (note that the generic type creation isn't strictly needed to see if the value is of the correct type, but it enforces the dictionary type to be Offset<>):
object GetInternalOffsetValue(int address, Type type)
{
object tempOffset;
var offsetBaseType = typeof(Offset<>);
var offsetType = offsetBaseType.MakeGenericType(type);
if (Offsets.TryGetValue(address, out tempOffset))
{
if (offsetType.IsInstanceOfType(tempOffset))
return tempOffset.GetType().GetProperty("Value").GetGetMethod().Invoke(tempOffset, null);
else
throw new InvalidOperationException(string.Format("Value of requested offset {0} does not match the expected type {1}", address, type));
}
var newOffset = Activator.CreateInstance(offsetType);
Offsets.Add(address, newOffset);
if (type.IsValueType)
return Activator.CreateInstance(type);
return null;
}
I am especially unfond of the magic string needed to get the value :)
If IOffset was public, the dictionary can be strongly typed, and the second method can be trivialized by comparing the data type directly instead of having to create a generic type on the fly, and the offset value can be accessed directly via IOffset.Value rather than relying on a reflected magic string "Value".