Should generic type checking be avoided? Do not use traditional type checking ( myvar is int) checks , but rather use the typecode type.
Using generics, with type checking, allows you to create a single method without parameters that supports the tasks of regular overloaded methods. This is a problem with methods without parameters; they cannot be overloaded.
public type Read<type>() {
switch( Type.GetTypeCode( typeof( type ) ) ) {
case System.TypeCode.Boolean:
bool bool_val = ( Buff[ Peek ] > 0 );
Peek = ( ++Peek + ( Align - 1 ) ) & ~( Align - 1 );
return (type)(object)bool_val;
case System.TypeCode.Byte:
byte byte_val = Buff[ Peek ];
Peek = ( ++Peek + ( Align - 1 ) ) & ~( Align - 1 );
return (type)(object)byte_val;
case TypeCode.Ushort:
ushort ushort_val = (ushort)( Buff[ Peek ] | ( Buff[ Peek + 1 ] << 8 ) );
Peek += 2;
Peek = ( Peek + ( Align - 1 ) ) & ~( Align - 1 );
return (type)(object)ushort_val;
break;
...
}
}
This seems like the only way to achieve a form of overload using parameterless methods. Is this a bad practice?
Kayle source
share