【发布时间】:2014-08-10 22:45:43
【问题描述】:
我正在使用 CodeDom 生成一些 C# 代码,基本上是尝试复制现有类的所有属性。代码相当简单:
//type is a Type
foreach(PropertyInfo p in type.GetProperties())
{
Type eType = p.PropertyType;
AddProperty(eType, p.Name);
// ...
}
void AddProperty(Type propType, string name)
{
CodeMemberProperty newProperty = new CodeMemberProperty();
newProperty.Type = new CodeTypeReference(propType);
newProperty.Name = name;
targetClass.Members.Add(newProperty);
}
这对字符串很有效,但对于可以为空的原始类型,例如 decimal? 和 int?,在生成的代码中我会得到以下内容:
public virtual System.Nullable<decimal> MyNullableDecimal
而不是
public decimal? MyNullableDecimal
我想不通。有什么建议吗?
【问题讨论】:
标签: c# properties nullable codedom