Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define C# aliases/keywords for my own classes like int, string, object?

Its not about using foo = MyPackage.Foo;

I realize this has more to do with the IDE (Visual Studio 2010) that shows me special types in blue. And I want the same for some of my classes. I want it blue (presented as special) and be accessible in the whole project.

The reason is that I want to give them meaning/importance so that everyone in my team knows this class is a key class of the whole project.

// "foo" and "Foo" shall be just the same type:
foo:     MyPackage.Foo

object:  System.Object
string:  System.String
bool:    System.Boolean
byte:    System.Byte
sbyte:   System.SByte
short:   System.Int16
ushort:  System.UInt16
int:     System.Int32
uint:    System.UInt32
long:    System.Int64
ulong:   System.UInt64
float:   System.Single
double:  System.Double
decimal: System.Decimal
char:    System.Char

Is that possible ? If there would be a file in the installation of my IDE and there would be the above keywords inside and I could add some keywords to it and save it and restart the IDE ?

To have 2 classes do the very same I could do this. It would be 2 names for the same class. I know I would need to cast between those.

public class foo : Foo
{ }
like image 985
Bitterblue Avatar asked Dec 05 '13 13:12

Bitterblue


2 Answers

If you want to define a keyword (like int or void) for such a class, it is not supported in C#. Keywords are predefined, reserved identifiers, and only a change to the compiler would make it possible to do what you intent.

The closest approach would be to specify a class/namespace alias, as shown by @Soner Gönül. However, since this applies to a single file, you would have to pre-include it in every project file, as expressed in this post.

Now, the keywords are colored by the IDE based on the fact that they are keywords (in the sense that they are listed somewhere in the IDE's syntax highlighting engine as keywords for the C# language). Depending on the IDE you are using, you might be able to add an identifier for that class to look like a keyword. However, you would still need to import an alias for it in each file, otherwise the compiler won't recognize it.

like image 130
rae1 Avatar answered Nov 03 '22 02:11

rae1


Your question seems to be about two distinct requirements:

  1. Define a "global" symbol/class, available everywhere.
  2. Apply a distinct color (i.e. syntax highlighting) for that symbol.

1) Not directly possible. What you could do in this case is providing an assembly containing that class, and enforce its usage by your team. You could even use a static analysis tool and trigger some compile errors (say, if that assembly is not referenced).

2) This is even harder to implement. Customizing the default syntax colorization mechanism for C# in Visual Studio is possible, but definitely not easy.

Visual studio supports a given language (with syntax highlighting, IntelliSense and so forth) through a Language Service. So it could be feasible to add a custom set of keywords, with a distinct color palette, by following these steps. You would also have to find out how to to plug your custom code into the already defined C# Language Service.

PS: Never tried doing that, and I get tired just looking at the work that seems to be needed. But I guess that's what the JetBrains ReSharper team did.

PPS: If you were using Managed C++ instead of C# it would be much simpler.

like image 30
rsenna Avatar answered Nov 03 '22 01:11

rsenna