在C#中的反思和运算符重载

在C#中的反思和运算符重载

问题描述:

这里的交易。我有一个程序,将加载给定程序集,所有的类型及其成员通过解析和编译一个TreeView(非常类似于旧MSDN网站),然后建立HTML页面在TreeView的每个节点。它基本上需要一个给定程序,并允许用户创建自己的MSDN般的图书馆为它的文档目的

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.

下面是我所碰到的问题:每当运算符重载是在定义的类中遇到,反射返回作为的MethodInfo设置为类似op_Assign或op_Equality的美称。我希望能够捕捉到这些,妥善一一列举了,但我无法找到返回给准确识别我正在寻找一个运营商的MethodInfo对象任何事情。

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.

我绝对不想只是拍摄以OP_开头的一切,因为这肯定会(在某种程度上)将拿起它不应该的方法。我知道,那是特殊情况像这样的其他方法和属性有IsSpecialName属性设置,但appearantly这不是与运营商的情况。

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.

该OP_命名约定为标准或.net的事实标准。反映后,我会做这样的事情:

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将交换机上所有重载运营商(不要忘了隐性和显性转换)。如果方法名称不是标准算的名字之一,它调用GenerateDocForStandardMethod。我找不到运营商的方法名称的一个详尽的清单,但我大概可以提供,如果你真的需要它的完整列表。

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.

编辑:
这里有一个列表重载操作符的方法名(从的 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

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