Reflection and Operator Overloads in C#

后端 未结 3 1563
悲&欢浪女
悲&欢浪女 2020-12-15 08:18

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

相关标签:
3条回答
  • 2020-12-15 08:43

    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.

    0 讨论(0)
  • 2020-12-15 08:54

    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

    0 讨论(0)
  • 2020-12-15 09:06

    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

    0 讨论(0)
提交回复
热议问题