私は問題に直面しています。object から short へのキャストは機能しません。
私が持っているクラスでは(単なる例):
public const uint message_ID = 110;
そして、別のクラスのコンストラクターには、次のものがあります。
Assembly asm = Assembly.GetAssembly(typeof(ProtocolTypeManager));
foreach (Type type in asm.GetTypes())
{
if (type.Namespace == null || !type.Namespace.StartsWith(typeof(MyClass).Namespace))
continue;
FieldInfo field = type.GetField("message_ID");
if (field != null)
{
short id = (short)(field.GetValue(type));
...
}
}
キャストまでは問題ありません。私のフィールドは null ではなく、field.GetValue(type) は適切なオブジェクト (オブジェクト値 = 110) を提供します。
どこかで、オブジェクトから int へのボックス化解除が機能することを読みました。まあ、試してみましたが、まだ機能しません。
object id_object = field.GetValue(type);
int id_int = (int)id_object;
short id = (short)id_object;
例外はこれです: http://puu.sh/5d2jR.png (フランス人には申し訳ありません。型またはキャストのエラーであると書かれています)。
解決策を持っている人はいますか?
ありがとう、ベリディタス。