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

Its not around using foo = MyPackage.Foo;

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

The reason is because I want to give them meaning / importance so that everyone on my team knows that 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 it possible? If there was a file in my IDE installation and inside the keywords listed above, and could I add some keywords to it and save it and restart the IDE?

To make 2 classes, I can do it too. That would be 2 names for the same class. I know that I will need to throw between them.

 public class foo : Foo { } 
+6
source share
5 answers

Your question seems to relate to two different requirements:

  • Define a "global" symbol / class available everywhere.
  • Apply a separate color (for example, syntax highlighting) for this character.

1) Directly. What you can do in this case is to provide an assembly containing this class and make sure that your team uses it. You can even use the static analysis tool and run some compilation errors (say, if this assembly is not specified).

2) This is even more difficult to implement. Setting the default syntax coloring engine for C # in Visual Studio is possible, but definitely not easy.

Visual studio supports this language (with syntax highlighting, IntelliSense, etc.) through the Language Service . Thus, it would be possible to add a custom set of keywords with a clear color palette by following these steps. You will also need to learn how to connect your code to an already installed C # language service.

PS: I never tried to do this, and I was tired, just looking at the work that seems necessary. But I think the JetBrains ReSharper team did .

PPS: If you used Managed C ++ instead of C #, that would be a lot easier .

+3
source

If you want to define a keyword (e.g. int or void ) for such a class, it does not support in C #. Keywords are predefined, reserved identifiers, and only a change in the compiler will allow you to do what you intend.

The closest approach is to specify an alias for the namespace / namespace, as shown by @Soner Gönül. However, since this applies to a single file, you need to first include it in each project file, as indicated in this post .

Keywords are now colored in the IDE based on the fact that they are keywords (in the sense that they are listed somewhere in the IDE syntax as keywords for C #). Depending on your IDE, you can add an identifier for this class to make it look like a keyword. However, you still need to import an alias for it in each file, otherwise the compiler will not recognize it.

+5
source

Are you looking for using directive like this?

 using foo = MyPackage.MyClass; 
Directive

EDIT : using is for namespaces or types only. What you look seems to me impossible, but I keep looking.

+2
source

The only way to achieve this is to create an assembly for reference in your project (or just create a class in your project if you do not want to have an external assembly).

In your assembly, you can remove the namespace and use it only with the class (types):

 public class mytype { public void DoSomething() { } } public class MyType : mytype { } 

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

Then, in your application, after adding it as a link, you can simply 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(); } } } 

It is assumed that the color will be a standard color, but will allow you to at least access your custom “type” that you defined throughout your project.

Hope this leads you in the right direction.

0
source

You cannot do what you are trying to do; for example, a string is an alias of System.String in C #; It’s not possible to add aliases to this list, as this will be due to a change in language.

0
source

Source: https://habr.com/ru/post/959384/


All Articles