Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Visual Studio to generate code using System types (Int32) instead of built-in aliases (int)

Can I get Visual Studio to transform the built-in aliases into the System types? For example, if I define the following interface

public interface IExample
{
    Int32 DoWork(String input);
}

and use VS to automatically generate the interface, I get the built-in types.

public class Demo : IExample
{
    public int DoWork(string input) { }
}

I want those to automatically change to the System types

public class Demo : IExample
{
    public Int32 DoWork(String input) { }
}

I'm not looking for a full installable solution, just a starting point. Can I write a script in VS that's hooked to text completion or on-save? Should I write an add-on that has a context menu item for projects - 'Convert aliases to System types'?


Note: I prefer the System types because they are formatted by VS like other types. Built-in aliases are formatted like keywords. Also, it's a coding style guideline at my current job.

Update: It's clear from MS that existing VS code-generation will always produce the built-in aliases.

like image 946
Anthony Mastrean Avatar asked Jan 19 '09 14:01

Anthony Mastrean


2 Answers

The built-in aliases are formatted like keywords because the are keywords. The compiler performs the translation from language data type keywords to underlying CLR type for you.

There is nothing in Visual Studio that will automatically do this translation for you so you will need to write your own utility that will do this. There are several different ways to do this - you can write a macro or a VS plugin - but (IMHO) none of them will be trivial to write and ensure correctness.

You state that this is both personal preference and coding style requirements. I think the formatting concerns seem misplaced. It is generally easier/cleaner/preferred to use the language aliases instead of the CLR types. Other than the syntax coloring in the text editor there is absolutely no difference between the two and really no reasons to use the CLR names for your types. What are the reasons (if any) given in the coding style requirements for forcing the use of the CLR types instead of the language aliases?

like image 194
Scott Dorman Avatar answered Oct 14 '22 20:10

Scott Dorman


The two types mean exactly the same thing, there is no danger that one will mean something different.

The only difference is how they look, if thats important to you, you can write a macro that will replace any built-in-aliases with System types.

like image 21
Binary Worrier Avatar answered Oct 14 '22 21:10

Binary Worrier