Generating code -- is there an easy way to get a proper string representation of nullable type?
Posted
by Cory Larson
on Stack Overflow
See other posts from Stack Overflow
or by Cory Larson
Published on 2010-03-17T02:35:26Z
Indexed on
2010/03/17
2:41 UTC
Read the original article
Hit count: 403
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 CodeTemplateEngine, with two derived classes VBTemplateEngine and CSharpTemplateEngine. This question regards creating the property signatures based on columns in a database table. Using the IDataReader's GetSchemaTable method I gather the CLR type of the column, such as "System.Int32", and whether it IsNullable. However, I'd like to keep the code simple, and instead of having a property that looks like:
public System.Int32? SomeIntegerColumn { get; set; }
or
public Nullable<System.Int32> SomeIntegerColumn { get; set; },
where the property type would be resolved with this function (from my VBTemplateEngine),
public override string ResolveCLRType(bool? isNullable, string runtimeType)
{
Type type = TypeUtils.ResolveType(runtimeType);
if (isNullable.HasValue && isNullable.Value == true && type.IsValueType)
{
return "System.Nullable(Of " + type.FullName + ")";
// or, for example...
return type.FullName + "?";
}
else
{
return type.FullName;
}
},
I would like to generate a simpler property. I hate the idea of building a Type string from nothing, and I would rather have something like:
public int? SomeIntegerColumn { get; set; }
Is there anything built-in anywhere, such as in the VBCodeProvider or CSharpCodeProvider classes that would somehow take care of this for me?
Or is there a way to get a type alias of int?
from a type string like System.Nullable'1[System.Int32]
?
Thanks!
© Stack Overflow or respective owner