7

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
{ }
Community
  • 1
  • 1
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • Are you looking for the ability to have stricter type safety? Because, after listening to a talk by Stroustrup (I believe [this one](http://channel9.msdn.com/Events/GoingNative/2013/Opening-Keynote-Bjarne-Stroustrup) I wanted in my next project to be able to describe what the string is, not just a string. In C++ you can use typedef but in C# I had to implement a wrapper class around string for each, which was a pain but these best I could find to do. – T. Kiley Dec 05 '13 at 13:30

5 Answers5

4

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.

Community
  • 1
  • 1
rae1
  • 6,066
  • 4
  • 27
  • 48
  • It's not the compiler, actually it is the IDE that is presenting keywords for me. Let's say if I can modify the list of keywords where int and void are in, I could make some of my classes appear the same ? – Bitterblue Dec 05 '13 at 13:41
  • Well, you are talking two different things here. The keywords are colored by the IDE based on the fact that they *are* keywords. Now, 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. – rae1 Dec 05 '13 at 13:47
  • Nice answer. But "the keywords are colored by the IDE based on the fact that they are keywords" is ambiguous IMHO. It can mean something obvious ("the C# team wrote the syntax highlighting rules based on the C# syntax rules") or something wrong ("Visual Studio is able to 'magically' identify the syntax highlighting rules based on the C# syntax"). – rsenna Dec 05 '13 at 15:01
  • I see what you were saying, and made a change accordingly. However, I don't imagine the second proposition to be too far off, and that's why I was trying not to make the assumption *IDE == Visual Studio* – rae1 Dec 05 '13 at 15:09
3

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.

Community
  • 1
  • 1
rsenna
  • 11,775
  • 1
  • 54
  • 60
2

Are you looking using directive like this?

using foo = MyPackage.MyClass;

EDIT: using directive is for only namespaces or types. What you looking sounds not possible to me but I keep searching..

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • No, I know this feature. – Bitterblue Dec 05 '13 at 13:28
  • 2
    I believe OP wants it available assembly-wide; furthermore, recognized as a keyword, which makes me think it is simply not possible. – rae1 Dec 05 '13 at 13:29
  • This is now possible in C# 10 with global usings. Though, the syntax highlighting will still not reflect it as a keyword. – Tim Nov 08 '21 at 05:52
0

The only way to really accomplish this is to create an assembly to reference into your project (or just create a class in your project if you don't want an external assembly).

In your assembly, you could remove the namespace and have it with just a class(es) like this:

public class mytype    
{
    public void DoSomething()
    {

    }
}
public class MyType : mytype
{

}

If you wanted both upper and lower case classes, just inherit the base class.

Then in your application, once you add it as a reference, you can just access it:

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            mytype my_type = new mytype();
            my_type.DoSomething();

            MyType my_Type = new MyType();
            my_Type.DoSomething();

        }
    }
}

Granted, the color will be the standard class color, but would allow you to at least access your custom "type" that you have defined throughout your project.

Hope this leads you in the right direction.

CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
0

You can't do what you're attempting to do; for example, string is an alias to System.String within the C# language; it isn't possible to add to that list of aliases, as that would involve modifying the language.

Allan Elder
  • 4,052
  • 17
  • 19