如何在 C# 的泛型函​​数中传递类型

How can I pass the type in a generic function in C#

我有以下代码:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
    public class SignalRHub : Attribute
    {
        public readonly string Route;

        public SignalRHub(string Route)
        {
            this.Route = Route;
        }
    }

    [SignalRHub("hubpath")]
    public class TestHub : Hub
    {
        ...
    }

这定义了一个带有属性的 SignalR 集线器以了解路径。

我想用 SignalRHub 属性动态注册集线器,所以我有以下代码来查找所有集线器:

        // find all hubs
        var Hubs =
            from Assemblies in AppDomain.CurrentDomain.GetAssemblies().AsParallel()
            from Types in Assemblies.GetTypes()
            let Attributes = Types.GetCustomAttributes(typeof(SignalRHub), true)
            where Attributes?.Length > 0
            select new { Type = Types };

        var HubsList = Hubs.ToList();

然后我想注册它们,但这是我遇到问题的地方:

        foreach (var H in HubsList)
        {
            // get the route attribute
            var Route = string.Empty;
            var Attributes = Attribute.GetCustomAttributes(H.Type);
            foreach (var Attribute in Attributes)
            {
                if (Attribute is SignalRHub A)
                {
                    Route = A.Route;
                    break;
                }
            }

            // register the hub
            if (string.IsNullOrEmpty(Route))
            {
                Logging.Warn($"[Hub] {H.Type.Name} does not have a path, skipping");
            }
            else
            {
                Logging.Info($"[Hub] Registering {H.Type.Name} with path {Route}");
                Application.UseSignalR(R => R.MapHub<H>(Route)); <- this won't compile
            }

MapHub 要求 T 从 Hub 派生; H 是 TestHub 类型,应该没问题,但该语法不起作用。

我怎样才能使这个工作?

编译器不满意,因为您将实例变量用作泛型类型。

因为H实例变量指向一个Hub实例你可以替换:

Application.UseSignalR(R => R.MapHub<H>(Route))

作者:

Application.UseSignalR(R => R.MapHub<Hub>(Route))

我的解决方案(使用反射)

using System;
using System.Reflection;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
//somewhere in your code
private static readonly MethodInfo mapperMethodInfo = 
    typeof(HubRouteBuilder).GetMethod(
        "MapHub",
        new Type [] { 
            typeof(PathString)
        },
        null
    );

// in your mapping code
// replace this:
Application.UseSignalR(R => R.MapHub<H>(Route));  

// with this
Application.UseSignalR(R => 
{
   // pay attention to use of H.Type, R and Route variables
   mapperMethodInfo.MakeGenericMethod(H.Type).Invoke(R, new object [] { Route });
});