C# 中的反射和运算符重载

发布于 2024-09-05 03:10:58 字数 460 浏览 4 评论 0原文

事情是这样的。我有一个程序,它将加载给定的程序集,解析所有类型及其成员并编译 TreeView(与旧的 MSDN 站点非常相似),然后为 TreeView 中的每个节点构建 HTML 页面。它基本上采用给定的程序集,并允许用户为其创建自己的类似 MSDN 的库以用于文档目的。

这是我遇到的问题:每当在定义的类中遇到运算符重载时,反射都会将其返回为“MethodInfo”,并将名称设置为“op_Assign”或“op_Equality”之类的内容。我希望能够捕获这些并正确列出它们,但我在返回的 MethodInfo 对象中找不到任何内容来准确识别我正在查看的运算符。

我绝对不想只捕获以“op_”开头的所有内容,因为这肯定会(在某些时候)会采用一种不应该采用的方法。我知道像这样的“特殊情况”的其他方法和属性具有“IsSpecialName”属性集,但显然运算符的情况并非如此。

我两天来一直在网上搜寻并绞尽脑汁试图解决这个问题,所以任何帮助将不胜感激。

Here's the deal. I've got a program that will load a given assembly, parse through all Types and their Members and compile a TreeView (very similar to old MSDN site) and then build HTML pages for each node in the TreeView. It basically takes a given assembly and allows the user to create their own MSDN-like library for it for documentation purposes.

Here's the problem I've run into: whenever an operator overload is encounted in a defined class, reflection returns that as a "MethodInfo" with the name set to something like "op_Assign" or "op_Equality". I want to be able to capture these and list them properly, but I can't find anything in the MethodInfo object that is returned to accurately identify that I'm looking at an operator.

I definitely don't want to just capture everything that starts with "op_", since that will most certainly (at some point) will pick up a method it's not supposed to. I know that other methods and properties that are "special cases" like this one have the "IsSpecialName" property set, but appearantly that's not the case with operators.

I've been scouring the 'net and wracking my brain to two days trying to figure this one out, so any help will be greatly appreciated.

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

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

发布评论

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

评论(3

手心的海 2024-09-12 03:10:58

op_ 命名约定是 .net 的标准或事实上的标准。在反映时,我会这样做:

public void GenerateDocForMethod(MethodInfo method)
{
    if(method.Name.StartsWith("op_"))
        GenerateDocForOperator(method);
    else
        GenerateDocForStandardMethod(method);
}

public void GenerateDocForOperator(MethodInfo method)
{
    switch(method.Name)
    {
        case "op_Addition":
        //generate and handle other cases...

        //handle methods that just happen to start with op_
        default:
            GenerateDocForStandardMethod(method);
    }
}

public void GenerateDocForStandardMethod(MethodInfo method)
{
    //generate doc
}

GenerateDocForOperator 将打开所有可重载运算符(不要忘记隐式和显式转换)。如果方法名称不是标准运算符名称之一,它将调用GenerateDocForStandardMethod。我找不到运算符方法名称的详尽列表,但如果您确实需要,我可能可以提供完整的列表。

编辑:
以下是可重载运算符的方法名称列表(取自 http://forums.devx.com/showthread.php?55322-Operator-Overloading.....C-can-do-it.. ..&p=208952#post208952):

op_Implicit
op_Explicit
op_加法
op_减法
op_乘
op_Division
op_Modulus
op_ExclusiveOr
op_BitwiseAnd
op_BitwiseOr
op_LogicalAnd
op_LogicalOr
op_Assign
op_LeftShift
op_RightShift
op_SignedRightShift
op_UnsignedRightShift
op_平等
op_GreaterThan
op_LessThan
op_不平等
op_GreaterThanOrEqual
op_LessThanOrEqual
op_乘法赋值
op_SubtractionAssignment
op_ExclusiveOrAssignment
op_LeftShiftAssignment
op_ModulusAssignment
op_AdditionAssignment
op_BitwiseAndAssignment
op_BitwiseOrAssignment
op_逗号
op_DivisionAssignment
op_Decrement
op_Increment
op_UnaryNegation
op_UnaryPlus
op_Ones 补码

The op_ naming convention is a standard or defacto standard for .net. When reflecting, I would do something like this:

public void GenerateDocForMethod(MethodInfo method)
{
    if(method.Name.StartsWith("op_"))
        GenerateDocForOperator(method);
    else
        GenerateDocForStandardMethod(method);
}

public void GenerateDocForOperator(MethodInfo method)
{
    switch(method.Name)
    {
        case "op_Addition":
        //generate and handle other cases...

        //handle methods that just happen to start with op_
        default:
            GenerateDocForStandardMethod(method);
    }
}

public void GenerateDocForStandardMethod(MethodInfo method)
{
    //generate doc
}

GenerateDocForOperator will switch on all of the overloadable operators (don't forget implicit and explicit conversions). If the method name is not one of the standard operator names, it calls the GenerateDocForStandardMethod. I couldn't find an exhaustive list of operator method names but I could probably provide a complete list if you really need it.

EDIT:
Here's a list of the method names of overloadable operators (taken from http://forums.devx.com/showthread.php?55322-Operator-Overloading.....C-can-do-it....&p=208952#post208952):

op_Implicit
op_Explicit
op_Addition
op_Subtraction
op_Multiply
op_Division
op_Modulus
op_ExclusiveOr
op_BitwiseAnd
op_BitwiseOr
op_LogicalAnd
op_LogicalOr
op_Assign
op_LeftShift
op_RightShift
op_SignedRightShift
op_UnsignedRightShift
op_Equality
op_GreaterThan
op_LessThan
op_Inequality
op_GreaterThanOrEqual
op_LessThanOrEqual
op_MultiplicationAssignment
op_SubtractionAssignment
op_ExclusiveOrAssignment
op_LeftShiftAssignment
op_ModulusAssignment
op_AdditionAssignment
op_BitwiseAndAssignment
op_BitwiseOrAssignment
op_Comma
op_DivisionAssignment
op_Decrement
op_Increment
op_UnaryNegation
op_UnaryPlus
op_OnesComplement

甜嗑 2024-09-12 03:10:58

运算符重载确实会将 IsSpecialName 标志设置为 true。
如果您通过显式地给它们一个像 op_* 这样的名称来实现这些方法,那么该标志将设置为 false。

Operator overloads do get the IsSpecialName flag set to true.
And if you implement the methods by explicitly giving them a name like op_* that flag is set to false.

花想c 2024-09-12 03:10:58

添加到 Wiser 的帖子:同一个线程 ( http://forums.devx.com/showthread.php?55322-Operator-Overloading.....C-can-do-it.... &p=208952#post208952 )提到了一些其他运算符:

Some additions - see CLI Partition 1, section 10.3.
 op_UnsignedRightShiftAssignment
 op_RightShiftAssignment
 op_MemberSelection
 op_PointerToMemberSelection
 op_LogicalNot
 op_True
 op_False
 op_AddressOf
 op_PointerDereference

这些运算符可以在《公共语言基础设施注释标准》一书中看到:http://books.google.ru/books?id=50PhgS8vjhwC&pg=PA111&lpg=PA111&dq=op_PointerToMemberSelection&source=bl&ots=vZIC0nA9sW&sig=4hTfAAWGkaKirgBQ4I1yBnK_D2M& ; hl=en&sa=X&ei=YsB2ULvAMuHx4QT25YCYAw&ved=0CB0Q6AEwAA#v=onepage&q=op_PointerToMemberSelection&f=false

还可以在这里找到一些方便的表格:http://en.csharp-online.net/Common_Type_System%E2%80%94Operator_Overloading

To add to the Wiser's post: the same thread ( http://forums.devx.com/showthread.php?55322-Operator-Overloading.....C-can-do-it....&p=208952#post208952 ) mentions some additional operators operators:

Some additions - see CLI Partition 1, section 10.3.
 op_UnsignedRightShiftAssignment
 op_RightShiftAssignment
 op_MemberSelection
 op_PointerToMemberSelection
 op_LogicalNot
 op_True
 op_False
 op_AddressOf
 op_PointerDereference

These operators are seen in The Common Language Infrastructure Annotated Standard book: http://books.google.ru/books?id=50PhgS8vjhwC&pg=PA111&lpg=PA111&dq=op_PointerToMemberSelection&source=bl&ots=vZIC0nA9sW&sig=4hTfAAWGkaKirgBQ4I1yBnK_D2M&hl=en&sa=X&ei=YsB2ULvAMuHx4QT25YCYAw&ved=0CB0Q6AEwAA#v=onepage&q=op_PointerToMemberSelection&f=false

Some handy table can also be found here: http://en.csharp-online.net/Common_Type_System%E2%80%94Operator_Overloading

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