intを特定の型に変換し、変換した型に依存する形式の文字列を返したいとします。
Typeオブジェクトを返すプロパティと、その型がTypeに依存する文字列を返すプロパティがあります。
コンパイラはなぜHexStringのコードを以下のようにしないのですか?
これを行うには同じように簡単な方法がありますか?C#ChangeTypeとToString?
public class TestClass
{
private int value;
public bool signed;
public int nBytes;
public int Value { get {return value;} set {this.value = value;}}
public Type Type {
get {
switch (this.nBytes) {
case 1:
return (this.signed ? typeof(SByte) : typeof(Byte));
case 2:
return (this.signed ? typeof(Int16) : typeof(UInt16));
default:
return null;
}
}
}
public String HexString {
get {
//?? compiler error: "no overload for method 'ToString' takes '1' arguments
return (Convert.ChangeType(this.Value, this.Type)).ToString("X" + this.nBytes);
}
}
}
これは答えではありません。ちょっとした提案です。ジェネリック医薬品を使用することがここで助けになるかもしれないと感じる。 –