Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中的泛型函数中传递类型#_C#_Signalr - Fatal编程技术网

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

C# 如何在C中的泛型函数中传递类型#,c#,signalr,C#,Signalr,我有以下代码: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class SignalRHub : Attribute { public readonly string Route; public SignalRHub(string Route) { this.Route = Route;

我有以下代码:

    [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
    {
        ...
    }
这定义了一个信号集线器,该集线器具有一个知道路径的属性

我希望使用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
            }
foreach(HubsList中的变量H)
{
//获取路由属性
var Route=string.Empty;
var Attributes=Attribute.GetCustomAttributes(H.Type);
foreach(属性中的var属性)
{
if(属性为SignalHub A)
{
路线=A.路线;
打破
}
}
//注册中心
if(string.IsNullOrEmpty(路由))
{
Logging.Warn($“[Hub]{H.Type.Name}没有路径,正在跳过”);
}
其他的
{
Logging.Info($“[Hub]将{H.Type.Name}注册到路径{Route}”);

Application.UseSignalR(R=>R.MapHub(Route));编译器不满意,因为您将实例变量用作泛型类型

因为H实例变量指向可以替换的集线器实例:

Application.UseSignalR(R => R.MapHub<H>(Route))
Application.UseSignalR(R=>R.MapHub(路由))
作者:

Application.UseSignalR(R=>R.MapHub(路由))
我的解决方案(使用反射)

使用系统;
运用系统反思;
使用Microsoft.AspNetCore.signal;
使用Microsoft.AspNetCore.Authorization;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Http.Connections;
//在代码中的某个地方
私有静态只读MethodInfo mapperMethodInfo=
typeof(HubRouteBuilder).GetMethod(
“地图中心”,
新类型[]{
类型(路径字符串)
},
无效的
);
//在映射代码中
//替换此项:
Application.UseSignalR(R=>R.MapHub(Route));
//用这个
Application.UseSignalR(R=>
{
//注意使用H.Type、R和Route变量
mapperMethodInfo.MakeGenericMethod(H.Type).Invoke(R,新对象[]{Route});
});

使用类型而不是变量,比如
Application.UseSignalR(R=>R.MapHub(Route));
您必须搜索接受2个参数系统的非通用MapHub。键入和路由不直接回答这个问题,但似乎您试图以一种奇怪的方式使用多态性。最好将每个属性包装在知道如何分派它的about类中,并且您的函数将只调用分派/路由方法然后属性创建者有责任选择正确的属性包装器。这是否意味着使用从(hub)派生的类型行吗?我不需要使用真正的类型?@AndriyShevchenko,没有非通用的MapHubcall@DrPhil,我不明白;你能举个例子吗?这不行,因为不能实例化类型“hub”。这里没有hub的实例(抽象类),您的TestHub继承了Hub类,使其成为Hub。C#泛型类型是不引用实例的类型。
Application.UseSignalR(R => R.MapHub<Hub>(Route))