Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define own keywords and meanings

Is it possible to define your keywords in C#?
I mean something like

public important Form newform;

where important would be the new keyword. It would mean something like, for example, that if the type is null when compiling an error occurrs. So the example would produce an error. An example with no error would be

public important Form newform = form1;
like image 670
Ian H. Avatar asked Dec 25 '15 15:12

Ian H.


People also ask

What is keywords and examples?

Keywords are the words and phrases that people type into search engines to find what they're looking for. For example, if you were looking to buy a new jacket, you might type something like “mens leather jacket” into Google. Even though that phrase consists of more than one word, it's still a keyword.

What are the 4 types of keywords?

When researching to discover a user's intentions behind making a search, we can classify all keywords into four main categories of intent: commercial, transactional, informational, and navigational. We're going to identify what these types are with a short breakdown of each type.

What are your keywords?

In terms of SEO, they're the words and phrases that searchers enter into search engines, also called "search queries." If you boil everything on your page — all the images, video, copy, etc. — down to simple words and phrases, those are your primary keywords.


1 Answers

No, the compiler doesn't allow you to add keywords out of box.

That doesn't mean you can't accomplish what you want, but it's going to be non trivial. You're going to need to perform your own custom static code analysis.

Roslyn

One way to do it is via the new .NET Compiler Services (Roslyn) Code Analyzers. You can read this MSDN article to get started: https://msdn.microsoft.com/en-us/magazine/dn879356.aspx and you'll end up with something that looks like this:

enter image description here

This will work from within Visual Studio, and you can generate at least Warnings, I would assume Errors as well (or you'll have to turn on "Treat Warnings as Errors").

Because you want to declaritively add the check to certain variables, you'll need to make use of Attributes:

/// <summary> Indicate a variable must be given a value 
/// in the same line it's declared </summary>
public class ImportantAttribute : Attribute {}

public class Program
{
   [Important]
   object thisShouldError;

   object thisIsFine;
}

Then when you write your Analyzer you'll need to check the variable declaration node to see if it's decorated with Important.

If you want this to work on a build server (or outside of VS), you might need to do some more work: Roslyn: workspace loads in console application but not in msbuild task

StyleCop

The easiest way is probably to use Code Analysis (ie StyleCop) to run Static Analysis Rule Sets against your code base:

enter image description here

You'll need to write your own Rule Set and I don't know if the Rule Sets will give you fidelity to be able to declare which variable declarations you want to force a check on, ie I don't know if you'll be able to mix & match this:

public class Program
{
   [Important]
   object thisShouldError;

   object thisIsFine;
}

Fody

Another approach is to use an IL weaving tool like Fody that lets you manipulate the IL of your application during compilation. AFAIK you can't directly generate compilation errors, but you might be able to add invalid IL that will in turn generate a compile time error.

So based on the same code as above, after a compilation pass with Fody you'd end up with code like:

public class Program
{
   [Important]
   object thisShouldError YouForgotToInitializeThisImportantVariable;

   object thisIsFine;
}

Custom Compiler

Microsoft did open source the C# compiler: https://github.com/dotnet/roslyn. So if you're feeling really ambitious, you can absolutely add in your own keywords in your own fork of the compiler. Of course then you'll essentially have your own language and it won't be able to compile on anything other than your custom compiler.

like image 193
Philip Pittle Avatar answered Sep 18 '22 05:09

Philip Pittle