【问题标题】:How to enable IDN/IRI Support for URI-Class by code?如何通过代码启用对 URI-Class 的 IDN/IRI 支持?
【发布时间】:2012-02-09 00:03:12
【问题描述】:

我正在尝试为 URI 类启用 IDN/IRI 支持,因为我需要德语变音符号域(例如 www.bücher.de)上的“Uri.IsWellFormedUriString”方法。

我在https://stackoverflow.com/a/6107682/413531 发现了类似的问题(取自“国际资源标识符支持”的http://msdn.microsoft.com/en-us/library/system.uri.aspx),但该解决方案对我不起作用。我当前的 app.config 文件如下所示:

<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="..." type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        <!-- ... some sections in here ... -->
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <!-- ... some sections in here ... -->
        </sectionGroup>
    </configSections>
    <userSettings>
        <!-- ... some settings in here ... -->
    </userSettings>
    <applicationSettings>
        <!-- ... some settings in here ... -->
    </applicationSettings>
</configuration>

当我刚刚添加时

  <uri>
  <idn enabled="All" />
  <iriParsing enabled="true" />
  </uri>

作为最后的另一个孩子,抛出异常:ConfigurationErrorsException - {"Das Konfigurationssystem konnte nicht initialisiert werden."}

所以我在http://msdn.microsoft.com/en-us/library/system.uri.aspx 中阅读了更多内容

Uri 类中的 IRI 和 IDN 处理也可以通过使用来控制 System.Configuration.IriParsingElement, System.Configuration.IdnElement 和 System.Configuration.UriSection 配置设置类。这 System.Configuration.IriParsingElement 设置启用或禁用 IRI 在 Uri 类中处理。 System.Configuration.IdnElement 设置启用或禁用 Uri 类中的 IDN 处理。这 System.Configuration.IriParsingElement 设置也间接 控制 IDN。必须启用 IRI 处理才能进行 IDN 处理 可能的。如果禁用 IRI 处理,则 IDN 处理将 设置为 .NET Framework 2.0 行为所在的默认设置 用于兼容性和不使用 IDN 名称。

不幸的是,我找不到 System.Configuration.IriParsingElement、System.Configuration.IdnElement 和 System.Configuration.UriSection 的使用示例。我不知道这些是如何使用的......

所以基本上,我的问题归结为:我想在 URI 类中启用 IDN/IRI 支持,但我不知道该怎么做。配置解决方案对我不起作用,所以我想通过代码尝试,但不知道如何。顺便提一句。我也想知道为什么配置不工作;)

【问题讨论】:

    标签: c# uri idn


    【解决方案1】:

    我找到了一个解决方案 - 至少对于 app.config 部分。根据http://social.msdn.microsoft.com/Forums/nl-NL/netfxnetcom/thread/56a73dbb-c0d4-4cad-876d-83ad74064393,如果使用 .Net 版本低于 4.0,则必须在 app.config 中添加一行:

    <configSections>
      <section name="uri" type="System.Configuration.UriSection, System,
                          Version=2.0.0.0, Culture=neutral,
                          PublicKeyToken=b77a5c561934e089" />
    </configSections>
    

    加上这行后,我也可以加

    <uri>
        <idn enabled="All" />
        <iriParsing enabled="true" />
    </uri>
    

    没有错误。我的完整 app.config 现在看起来像这样:

    <configuration>
        <configSections>
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="..." type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        <!-- ... some sections in here ... -->
            </sectionGroup>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <!-- ... some sections in here ... -->
            </sectionGroup>
    
    <!-- ... insert Missing section ... -->
    
            <section name="uri" type="System.Configuration.UriSection, System,
                          Version=2.0.0.0, Culture=neutral,
                          PublicKeyToken=b77a5c561934e089" />
    
        </configSections>
    
    <!-- ... insert URI settings ... -->
    
        <uri>
            <idn enabled="All" />
            <iriParsing enabled="true" />
        </uri>
    
        <userSettings>
        <!-- ... some settings in here ... -->
        </userSettings>
        <applicationSettings>
        <!-- ... some settings in here ... -->
        </applicationSettings>
    </configuration>
    

    【讨论】:

      【解决方案2】:

      这是我的解决方案,经测试有效。

      基本上,你需要改变System.Uri的静态非公共字段的值:

      • s_IdnScope
      • s_IriParsing

        public static bool ToggleIDNIRISupport(bool enable)
        {
            //Important: Try IsWellFormedUriString() once to initialize static fields: s_IdnScope, s_IriParsing
            Uri.IsWellFormedUriString("http://example.com/query=ü", UriKind.Absolute);
        
            //Get the assembly that contains the class
            Assembly assembly = Assembly.GetAssembly(typeof(Uri));
            if (assembly != null)
            {
                //Use the assembly in order to get the type of the class
                Type uriType = assembly.GetType("System.Uri");
                if (uriType != null)
                {
                    object idnScope = uriType.InvokeMember("s_IdnScope", BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic, null, null, new object[] { });
                    if (idnScope != null)
                    {
                        if (enable)
                        {
                            uriType.InvokeMember("s_IdnScope", BindingFlags.Static | BindingFlags.SetField | BindingFlags.NonPublic, null, null, new object[] { UriIdnScope.All });
                        }
                        else
                        {
                            uriType.InvokeMember("s_IdnScope", BindingFlags.Static | BindingFlags.SetField | BindingFlags.NonPublic, null, null, new object[] { UriIdnScope.None });
                        }
                    }
        
                    object iriParsing = uriType.InvokeMember("s_IriParsing", BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic, null, null, new object[] { });
                    if (iriParsing != null)
                    {
                        uriType.InvokeMember("s_IriParsing", BindingFlags.Static | BindingFlags.SetField | BindingFlags.NonPublic, null, null, new object[] { enable });
                    }
                }
            }
        
            return true;
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 2021-09-27
        • 1970-01-01
        • 2013-03-12
        • 1970-01-01
        相关资源
        最近更新 更多