简体   繁体   English

适用于.Net和C#扩展的Python-Python中的SignalR客户端(On方法不起作用)

[英]Python for .Net and C# extensions - SignalR client in Python (On method not working)

I'm trying to access Microsoft.SignalR library from Python for .Net in Visual Studio, and some of the implementation of a Hub (IHubProxy) is in an extension: HubProxyExtensions 我正在尝试从Python for Visual Studio中的.Net访问Microsoft.SignalR库,并且扩展的某些Hub(IHubProxy)实现:HubProxyExtensions

Python for .Net does not seem to cover extensions in the readme, and I can't find any reference (my google fu is not working in this case.) 适用于.Net的Python似乎没有涵盖自述文件中的扩展名,并且我找不到任何引用(在这种情况下,我的Google Fu无法正常工作。)

I'm able to load the extensions class directly, but calling the method fails: 我可以直接加载扩展类,但是调用方法失败:

ext = SignalR.Client.HubProxyExtensions
ext.On(self._proxy, method, handle)

The extension class loads and reports as a meta class and has methods from the immediate window 扩展类作为元类加载和报告,并具有直接窗口中的方法

ext
<CLR Metatype>
dir(ext)
['Equals', 'Finalize', 'GetHashCode', 'GetType', 'GetValue', MemberwiseClone', 'Observe', 'On', 'Overloads', 'ReferenceEquals', 'ToString', __call__', '__class__', '__cmp__', ...]

Calling ext.On() crashes the process. 调用ext.On()会使进程崩溃。

Does Python for .Net support extensions at all? Python for .Net是否完全支持扩展? (Or is it just a problem specific to this particular implementation) (或者这仅仅是特定于此特定实现的问题)


Update: Doing a simple test on extensions, it looks like Python for .Net does handle extensions properly, so there is something wrong with my call to Signalr specifically... more testing to come. 更新:对扩展进行了简单的测试,看起来像.NET的Python可以正确处理扩展,因此我对Signalr的调用有问题,特别是在测试中。

So I'm going to answer my own question since it turned out to be multiple issues; 因此,我将回答我自己的问题,因为它原来是多个问题。 and I didn't see others addressing this directly. 而且我没有看到其他人直接解决这个问题。


Action templates : 动作模板

The problem in my case is there is no way to bind Action templates in Python for .Net. 就我而言,问题是没有办法在Python for .Net中绑定Action模板。 So there is no way to call the correct On methods in a hub. 因此,无法在集线器中调用正确的On方法。

Someone else found the same issue recently 最近有人发现了相同的问题


Extensions : 扩展名

Python for .Net can call extension methods; 适用于.Net的Python可以调用扩展方法; Simply call the static extension method through the extension class like any other static class methods. 像其他任何静态类方法一样,只需通过扩展类调用静态扩展方法即可。

C# C#

namespace ExtensionTest
{
    public class MyClass
    {
        public string MyMethod(string something)
        {
            System.Console.Out.WriteLine(string.Format("Hi! This is something! {0}", something));
            return something + " something else";
        }
    }
}

namespace ExtensionTest
{
    public static class MyExtension
    {
        public static string MyExtensionMethod(this MyClass myclass, string something)
        {
            System.Console.Out.WriteLine(string.Format("This is the extension {0}", something));
            string somethingelse = myclass.MyMethod(something);
            return somethingelse + " more of something";
        }
    }
}

Python: 蟒蛇:

import clr
clr.AddReference('ExtensionTest')
import ExtensionTest
obj = ExtensionTest.MyClass()
s = obj.MyMethod('Hi')
print s
s = ExtensionTest.MyExtension.MyExtensionMethod(obj, 'Hello!')

Output: 输出:

Hi! This is something! Hi
Hi something else
This is the extension Hello!
Hi! This is something! Hello!
Hello! something else more of something

I'm not sure why this isn't mentioned on the readme for python for .net. 我不确定为什么在.net的python自述文件中没有提到这一点。

I guess it's obvious :) 我想这很明显 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM