views:

148

answers:

2

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.

+2  A: 

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://dotnet.itags.org/dotnet-tech/116035/): 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

Wesley Wiser
That's a great idea of how to handle it, actually. Thanks. Guess I got too hung up on finding an defining value to identify the operator. Incidentally, I wonder what would happen if I made a class with an addition operator overload *and* a random method called "op_Addition". I wonder if the compiler will let you do that? Sounds like an experiment for the next few minutes to me :)Thanks, again. Happen to know where I can get a list of all possible op_XXXX values?
TenshiNoK
I had trouble finding the list, too. You rock :)
TenshiNoK
+1  A: 

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.

ajm