/// <summary> /// プライベート変数の取得. /// </summary> /// <param name="obj">Object.</param> /// <param name="privateName">Private name.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> protected T GetPrivate<T>(object obj, string privateName) { System.Reflection.FieldInfo fieldInfo = obj.GetType().GetField(privateName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); return (T)fieldInfo.GetValue(obj); }
fieldInfo.GetValue(obj) as T;
だと
error CS0413: The `as' operator cannot be used with a non-reference type parameter `T'. Consider adding `class' or a reference type constraint
になる。
T
が参照型だからではないからか?