2

I know in C#, as hybrid language, got operators, that aren't objects, like in smalltalk.

Is there a way to treat them as objects (like in Smalltalk)? I mean, add attributes and methods to them, or include them in a class hierarchy.

As I far as I know, (thanks to Google), it's possible to overload an operator, but I can't find anything else.

For example, I want to know if it's possible todo something like

if(+.PrecedenceLevel > *.PrecedenceLevel) 
{
//something
}

Is just a question of my curiosity, not for my work.

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
  • 2
    No, that isn't possible. – Marc Gravell Sep 03 '12 at 16:23
  • You mean something like `public seal class +{...}` ?? then what will `1+2` do??? – perilbrain Sep 03 '12 at 16:26
  • As I mentionet to Jon Skeet, let's suppose I want to do this if(+.PrecedenceLevel > *.PrecedenceLevel) {something} But you're right, I didn't consider what 1+2 will do. But I guess that's just implementation, right ? I mean, in Smalltalk, it's possible, as far as I've read – Gonzalo.- Sep 03 '12 at 16:32
  • 1
    @ElVieejo:- whatever but this `if(+.PrecedenceLevel > *.PrecedenceLevel) {something}` concepts is really good, wish somebody will implement someday :) :thumbsup: – perilbrain Sep 03 '12 at 16:59

2 Answers2

3

Overloaded operators are static methods, and hence have corresponding MethodInfo objects that can be manipulated or Invoked:

var eqStr = typeof(string).GetMethod("op_Equality");
Console.WriteLine(eqStr.Name);//op_Equality - the .NET name rather than the C# name
Console.WriteLine(eqStr.IsSpecialName);//True - languages may give it a different name
Console.WriteLine(eqStr.Invoke(null, new object[]{"abc", "abc"}));//True
Console.WriteLine(eqStr.Invoke(null, new object[]{"abc", "def"}));//False

This really only let's us get at the language-neutral features of the method though, which could be useful, but it doesn't let us at things like which name a given language uses (== in C#, =in VB.NET, etc), its precedence or anything "operatory" so to speak.

And it's only for the overloads, not the built-ins.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • 2
    I didn't know that .Net treats the operator this way. Very interesting ! – Gonzalo.- Sep 03 '12 at 16:58
  • 2
    @ElVieejo again though, only the overloads. the `+` of `1 + 2` cannot be accessed in this way as it's defined in the language not the BCL. Similarly, while this tells us that a language might give it a "special name", it doesn't tell us that C# does, or that that name is `==`. – Jon Hanna Sep 03 '12 at 17:01
2

The short answer is "no", operators are not objects in C# and even the operator overloading mechanism via static methods seems to be kind of a weak design, so you'd rarely see sexy examples of C# operator overloading.

But, if you're curios, in addition to Reflection already mentioned by Jon, there are a couple more meta-programming mechanisms in .NET, in which operators are represented by objects. You still won't be able to give them any additional attributes or check the precedence, but that's probably the closest thing you can get in .NET to what you describe.

One way is through working with Code DOM, there is a CodeBinaryOperationExpression class. When you read or create one, you specify the operator using CodeBinaryOperatorType enum. For example, you can make a method in C# which will in run-time create an expression tree with a dynamically chosen operator, compile it into a lambda and pass back to the caller to be used as a common function.

Another way, opposite to the previous in some sense, is by using the new Roslyn API, which provides code parsing capabilities. You can give some source code to Roslyn, it will turn it into the expression tree and you can write visitors or syntax rewriters to modify that tree. For example, you may go over the tree and substitute all "plus" operators with "minus" operators (or with any code of your choice).

You can find more details and examples here and here.

HTH.

Community
  • 1
  • 1
Massimiliano
  • 16,770
  • 10
  • 69
  • 112
  • this is very interesting. I've heard about expression tree, but never used, and I didn't know how to use them. Thanks ! +1 – Gonzalo.- Sep 04 '12 at 15:51