Example #1
0
        /// <summary>
        /// Get the insertion position index of the caret.
        /// </summary>
        private static int GetCaretIndex(TextSelection selection)
        {
            if (selection == null)
            {
                return(0);
            }

            var tTextRange = selection.GetType().GetInterfaces().FirstOrDefault(i => i.Name == "ITextRange");
            var oStart     = tTextRange?.GetProperty("Start")?.GetGetMethod()?.Invoke(selection, null);
            var value      = oStart?.GetType().GetProperty("Offset", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(oStart, null) as int?;

            return(value.GetValueOrDefault(0));
        }