Skip to content

CharUnicodeInfo.GetBidiCategory(String, Int32) returns enumeration value that is not listed #55511

@Lotes

Description

@Lotes

Description

I need the (Unicode) bidirectional category for a character of a string (left-to-right or right-to-left, basically).
In a tutorial, this is accomplished by accessing the internal function System.Globalization.CharUnicodeInfo.GetBidiCategory(String, Int32) using reflection.

If you execute the following code, the return value of several calls will output a 64 instead of a valid enum value that is listed in the internal enumeration.

using System;
using System.Globalization;
using System.Reflection;

namespace ConsoleApp4
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var st = "Hello!\r\nשלום!";
            var typeCharUnicodeInfo = Type.GetType("System.Globalization.CharUnicodeInfo");
            var bf = BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.InvokeMethod;
            var getBidiCategory = typeCharUnicodeInfo.GetMethod("GetBidiCategory", bf, null, new []{typeof(string), typeof(int)}, null);

            for (var ich = 0; ich < st.Length; ich++)
            {
                var parameters = new object[2] {st, ich};

                var o = getBidiCategory.Invoke(typeCharUnicodeInfo, bf, null, parameters, CultureInfo.InvariantCulture);

                Console.WriteLine("U+" + ((ushort) st[ich]).ToString("x4") + "    " + o.GetType() + "    " + o);
            }
        }
    }
}

But what I actually inspect is a value like "RightToLeft".

My target platform is net5.0.

It is not urgent, just an entry in your tracking system.
I can also compute the information on my own with this file here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.GlobalizationenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions