Specifying a Type Alias ​​with CodeDom

I am dynamically generating C # code using CodeDom. I want to declare a type alias in a namespace. Sort of:

namespace MyNameSpace
{
   using Timer = System.Threading.Timer; 
   ...
}

I can create a namespace, but I don’t know how to create a type alias. Code so far:

CodeCompileUnit unitCompile = new CodeCompileUnit();
CodeNamespace nsScript = new CodeNamespace("MyNamespace");
unitCompile.Namespaces.Add(nsScript);

How to add "using timer = System.Threading.Timer;" into the namespace?

+3
source share
1 answer

You can use directly in the class CodeNamespaceImport.

CodeNamespaceImport cd = 
    new CodeNamespaceImport("Timer = System.Threading.Timer");

It will generate such classes.

using Timer = System.Threading.Timer;

I tried with VB.Net and it works. I have not tried with C #.

+5
source

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


All Articles