生成代码-是否有一种简单的方法来获取可为null的类型的正确字符串表示形式? [英] Generating code -- is there an easy way to get a proper string representation of nullable type?

查看:82
本文介绍了生成代码-是否有一种简单的方法来获取可为null的类型的正确字符串表示形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在构建一个应用程序,该应用程序将使用C#和VB输出(取决于项目设置)来生成大量代码。

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).

我们有一个CodeTemplateEngine,它带有两个派生类VBTemplateEngine和CSharpTemplateEngine。这个问题涉及基于数据库表中的列创建属性签名。使用IDataReader的GetSchemaTable方法,我收集了列的CLR类型,例如 System.Int32,以及它是否为IsNullable。但是,我想使代码保持简单,而不要使用看起来像这样的属性:

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; }

    public Nullable<System.Int32> SomeIntegerColumn { get; set; },

使用此函数(从我的VBTemplateEngine)解析属性类型的地方,

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;
        }
    },

我想生成一个更简单的属性。我讨厌从零开始构建Type字符串的想法,而宁愿有类似这样的东西:

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; }

任何地方都有内置的东西,例如在VBCodeProvider或CSharpCodeProvider类中会以某种方式使用

Is there anything built-in anywhere, such as in the VBCodeProvider or CSharpCodeProvider classes that would somehow take care of this for me?

还是有办法从类型中获取 int的类型别名? System.Nullable'1 [System.Int32] 这样的字符串?

Or is there a way to get a type alias of int? from a type string like System.Nullable'1[System.Int32]?

谢谢!

更新:

找到了可以做到的,但是我仍然对Type全名与其别名的映射类型保持警惕。

Found something that would do, but I'm still wary of that type of mapping of Type full names to their aliases.

推荐答案

您可以使用CodeDom对泛型类型的支持和GetTypeOutput方法来做到这一点:

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);

这将尊重特定于语言的类型关键字,例如C# int 或VB Integer ,尽管它仍会为您提供 System.Nullable< int> 而不是 int?

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?.

这篇关于生成代码-是否有一种简单的方法来获取可为null的类型的正确字符串表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆