Repository: AvaloniaUI/Avalonia
File: src/Avalonia.Base/Data/Core/Plugins/ObservableStreamPlugin.cs (C#)

Code before:
﻿// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;

namespace Avalonia.Data.Core.Plugins
{
    /// <summary>
    /// Handles binding to <see cref="IObservable{T}"/>s for the '^' stream binding operator.
    /// </summary>
    public class ObservableStreamPlugin : IStreamPlugin
    {
        /// <summary>
        /// Checks whether this plugin handles the specified value.
        /// </summary>
        /// <param name="reference">A weak reference to the value.</param>
        /// <returns>True if the plugin can handle the value; otherwise false.</returns>
        public virtual bool Match(WeakReference reference) => reference.Target is IObservable<object>;

        /// <summary>
        /// Starts producing output based on the specified value.
        /// </summary>
        /// <param name="reference">A weak reference to the object.</param>
        /// <returns>
        /// An observable that produces the output for the value.
        /// </returns>
        public virtual IObservable<object> Start(WeakReference reference)
        {
            return reference.Target as IObservable<object>;
        }
    }
}


Review comment (suggestion):
Another option here is to invoke the `Observable.Cast` method.

Code after:

        /// <summary>
        /// Starts producing output based on the specified value.
        /// </summary>
        /// <param name="reference">A weak reference to the object.</param>
        /// <returns>
        /// An observable that produces the output for the value.
        /// </returns>
        public virtual IObservable<object> Start(WeakReference reference)
        {
            var target = reference.Target;

            // If the observable returns a reference type then we can cast it.
            if (target is IObservable<object> result)
            {
                return result;
            };

            // If the observable returns a value type then we need to call Observable.Select on it.
            // First get the type of T in `IObservable<T>`.
            var sourceType = reference.Target.GetType().GetInterfaces().First(x =>
                  x.IsGenericType &&
                  x.GetGenericTypeDefinition() == typeof(IObservable<>)).GetGenericArguments()[0];

            // Get the Observable.Select method.
            var select = GetObservableSelect(sourceType);

            // Make a Box<> delegate of the correct type.
            var funcType = typeof(Func<,>).MakeGenericType(sourceType, typeof(object));
            var box = GetType().GetMethod(nameof(Box), BindingFlags.Static | BindingFlags.NonPublic)
                .MakeGenericMethod(sourceType)
                .CreateDelegate(funcType);

            // Call Observable.Select(target, box);
            return (IObservable<object>)select.Invoke(
                null,
                new object[] { target, box });
        }

        private static MethodInfo GetObservableSelect(Type source)
        {
            return GetObservableSelect().MakeGenericMethod(source, typeof(object));
        }

        private static MethodInfo GetObservableSelect()
        {
            if (observableSelect == null)
            {
                observableSelect = typeof(Observable).GetRuntimeMethods().First(x =>
                {
                    if (x.Name == nameof(Observable.Select) &&