gpt4 book ai didi

c# - 如何通过代码启用 URI 类的 IDN/IRI 支持?

转载 作者:行者123 更新时间:2023-11-30 19:46:16 25 4
gpt4 key购买 nike

我正在尝试为 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>

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

所以我进一步阅读了 http://msdn.microsoft.com/en-us/library/system.uri.aspx遇到了

IRI and IDN processing in the Uri class can also be controlled using the System.Configuration.IriParsingElement, System.Configuration.IdnElement, and System.Configuration.UriSection configuration setting classes. The System.Configuration.IriParsingElement setting enables or disables IRI processing in the Uri class. The System.Configuration.IdnElement setting enables or disables IDN processing in the Uri class. The System.Configuration.IriParsingElement setting also indirectly controls IDN. IRI processing must be enabled for IDN processing to be possible. If IRI processing is disabled, then IDN processing will be set to the default setting where the .NET Framework 2.0 behavior is used for compatibility and IDN names are not used.

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

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

最佳答案

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

基本上,您需要更改 System.Uri 的静态非公共(public)字段的值:

  • 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;
    }

关于c# - 如何通过代码启用 URI 类的 IDN/IRI 支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8839487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com