現在のシステムのResponse符号化を取得

1220 ワード

/// <summary>
        ///  Response 
        /// </summary>
        /// <returns></returns>
        public static Encoding GetEncoding()
        {
            Encoding encoding = Encoding.UTF8;

            object config = WebConfigurationManager.GetWebApplicationSection("system.web/globalization");
            Type type1 = config.GetType();

            if (type1 != null)
            {
                PropertyInfo pinfo = type1.GetProperty("responseEncoding", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (pinfo != null)
                {
                    encoding = (Encoding)pinfo.GetValue(config, null);
                }
                else
                {
                    FieldInfo finfo = type1.GetField("responseEncoding", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (finfo != null)
                    {
                        encoding = (Encoding)finfo.GetValue(config);
                    }
                }
            }

            return encoding;
        }