方法名称中的连字符,是否可以在任何 .NET 语言中使用?

发布于 2024-08-17 23:47:09 字数 428 浏览 9 评论 0原文

我需要支持名为“send-request”的方法作为在 XSLT 转换中使用的扩展函数。这是由 XslCompiledTransform 上的扩展对象提供的。扩展对象与 相比,最酷的一点是,我不必使用 ,您只需声明命名空间并调用函数。不好的是函数名必须与 CIL 方法名完全匹配。

所以,我想知道,是否有一种 .NET 语言支持方法名称中的连字符?不需要遵守 CLS,该方法是使用反射调用的。

或者,我可以使用某种修改程序集的 IL 的技术来更改方法名称吗?

或者,有没有办法拦截反射 GetMethod 调用并欺骗调用者“send-request”方法存在,但返回“SendRequest”方法?

I need to support a method named 'send-request' as an extension function to be used in an XSLT transformation. This is provided by an extension object on XslCompiledTransform. The cool thing about extension objects vs. <msxsl:script> is, well, that I don't have to use <msxsl:script>, you just declare the namespace and call the function. The bad thing is that the function name must match exactly the CIL method name.

So, I'm wondering, is there a .NET language that supports hyphens in method names ? CLS compliance is not required, the method is invoked using reflection.

Or, can I use some technique that modifies the IL of an assembly to change the method name ?

Or, is there a way to intercept the reflection GetMethod call and trick the caller that a method 'send-request' exists, but return a 'SendRequest' method instead ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

童话里做英雄 2024-08-24 23:47:09

您可以使用反射来做到这一点。

我使用 ILAsm 将以下内容编译为程序集(我删除了实际编译所需的所有内容;这些只是显着位(双关语)):

.method private hidebysig static int32 
'Te-st'() cil managed {
    // Code size       8 (0x8)
    .maxstack  1
    .locals init ([0] int32 CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldc.i4.s 0x11
    IL_0002:  stloc.0
    IL_0003:  br.s       IL_0005

    IL_0005:  ldloc.0
    IL_0006:  ret
}

我在名为 Program 在名为 Test 的命名空间中。请注意,方法名称用单引号括起来。这是根据规范 (ECMA #335):

标识符用于命名实体。简单标识符相当于 ID。然而,ILAsm 语法
允许使用可以使用 Unicode 字符集形成的任何标识符(请参阅分区 I)。 实现
这个,标识符应放在单引号内。

然后:

var assembly = Assembly.LoadFrom(path);
var method = assembly.GetType("Test.Program")
                     .GetMethod(
                         "Te-st",
                         BindingFlags.Static | BindingFlags.NonPublic
             );
Console.WriteLine(method.Invoke(null, null));

产生的输出:

17

我怀疑这在通常的 .NET 语言中是可能的。我不知道有任何语言允许/要求您“转义”标识符。如果没有这个,您能否想象尝试消除以下歧义:

int a;
int b;
int a-b;
int diff = a-b; // is that a minus b or the variable a-b?

也许有一个 COBOL.NET 您可以在其中执行此操作,我不知道。

You can do this using reflection.

I compiled the following into an assembly using ILAsm (I cut out all the fluff necessary to make this actually compile; these are just the salient bits (pun intended)):

.method private hidebysig static int32 
'Te-st'() cil managed {
    // Code size       8 (0x8)
    .maxstack  1
    .locals init ([0] int32 CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldc.i4.s 0x11
    IL_0002:  stloc.0
    IL_0003:  br.s       IL_0005

    IL_0005:  ldloc.0
    IL_0006:  ret
}

I defined this method in a class named Program in a namespace named Test. Note that the method name is surrounded by single quotes. This is per the spec (ECMA #335):

Identifiers are used to name entities. Simple identifiers are equivalent to an ID. However, the ILAsm syntax
allows the use of any identifier that can be formed using the Unicode character set (see Partition I). To achieve
this, an identifier shall be placed within single quotation marks.

Then:

var assembly = Assembly.LoadFrom(path);
var method = assembly.GetType("Test.Program")
                     .GetMethod(
                         "Te-st",
                         BindingFlags.Static | BindingFlags.NonPublic
             );
Console.WriteLine(method.Invoke(null, null));

This produced output:

17

I doubt that is the possible in the usual .NET languages. I don't know of any languages that allow/require you to "escape" identifiers. Without that, could you imagine trying to disambiguate the following:

int a;
int b;
int a-b;
int diff = a-b; // is that a minus b or the variable a-b?

Maybe there's a COBOL.NET where you could do this, I don't know.

南风几经秋 2024-08-24 23:47:09

标识符名称中的连字符在 Lisp 和 Scheme 中非常常见,因此我怀疑 IronScheme 本身就支持它,至少。在 Ruby 中创建这样的方法也很容易,尽管连字符在 Ruby 标识符中实际上是非法的,因此您可以使用 IronRuby

另外,C# 中的 @ escape-thingy 允许您使用其他非法标识符,但我不太清楚如何在这种情况下使用它。

Hyphens in identifier names are pretty common in Lisp and Scheme, and thus I suspect it is natively supported in IronScheme, at least. It's also pretty easy to create methods like this in Ruby, despite the fact that hypens are actually illegal in Ruby identifiers, so you could use IronRuby.

Also, there's the @ escape-thingy in C# that allows you to use otherwise illegal identifiers, but I couldn't quite figure out how to use it for this case.

风吹雨成花 2024-08-24 23:47:09

我认为您可以在这里做的最简单的事情就是直接在 IL 中定义方法。首先定义一个抽象类,其中包含您想要的所有其他方法。然后在 IL 中定义一个包含连字符的方法名称的派生类,并将其编译到一个单独的库中。

I think the simplest thing you could do here is define the method directly in IL. First define an abstract class which has every other method you want. Then define a derived class in IL which contains the hyphen'd method name and compile it into a separate library.

秋日私语 2024-08-24 23:47:09

我有理由确信 CLR 语言规范的一部分是它们必须能够处理运行时支持的任何名称,即使它恰好是该语言中的保留关键字或其他非法名称。

假设是这种情况,如果运行时支持方法名称中的连字符,那么所有 CLR 语言都应该能够处理它们,即使它需要诸如 VB.NET 的 [] 语法来声明或使用它们。

I'm reasonably sure that part of the spec for CLR languages is that they must be able to handle any name that the runtime supports, even if it happens to be a reserved keyword in that language or otherwise illegal.

Assuming that's the case, if the runtime supports hyphens in method names, then all CLR languages should be able to handle them, even if it requires something like e.g. VB.NET's [] syntax to declare or use them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文