template <typename T>
void Write(T data) {
int size = T.SizeInBytes;
// ...
}
And I can call Write() with any type that contains an integer-compatible member variable named 'SizeInBytes'. If one tries to do the same in C#:
public void Write<T>(T data) where T : struct {
int size = T.SizeInBytes;
// ...
}
Then it errors out with "error CS0119: Expression denotes a `type parameter', where a `variable', `value' or `type' was expected". Quite reasonably, since C# doesn't allow the use of expressions with generic types which can't be applied solely given the constraints.
Are there any common idioms to working around this? Assuming that (a) I need to work with value types, and (b) these structs are defined in a 3rd party library (i.e. I can't modify the definition of the struct itself).