So I\'m building an application that is going to do a ton of code generation with both C# and VB output (depending on project settings).
I\'ve got a CodeTemplateEngi
You can do this using CodeDom's support for generic types and the GetTypeOutput method:
CodeTypeReference ctr;
if (/* you want to output this as nullable */)
{
ctr = new CodeTypeReference(typeof(Nullable<>));
ctr.TypeArguments.Add(new CodeTypeReference(typeName));
}
else
{
ctr = new CodeTypeReference(typeName);
}
string typeName = codeDomProvider.GetTypeOutput(ctr);
This will respect language-specific type keywords such as C# int
or VB Integer
, though it will still give you System.Nullable<int>
rather than int?
.
There are two issues here:
System.Int32
has the C# alias int
, which you prefer.System.Nullable
can be indicated using a ?
symbol in C#, which you prefer.There are no methods included with the .NET Framework to take these into account when converting the type name to a string. You are going to have to roll your own.