Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection and Operator Overloads in C#

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.

like image 723
Mike U Avatar asked Jun 10 '10 16:06

Mike U


2 Answers

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

like image 114
Wesley Wiser Avatar answered Nov 16 '22 00:11

Wesley Wiser


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.

like image 30
ajm Avatar answered Nov 16 '22 00:11

ajm