2011-02-11 25 views
9

我正在EF 4.0中自定義我的.tt文件。現在作爲f定製的一部分,如果屬性類型爲Nullable<System.DateTime>System.DateTime,我需要向POCO類生成中的屬性添加一些代碼。我無法找到適合比較的語法。如何檢查T4模板文件中實體中屬性的數據類型

我在.tt文件中有以下代碼。

foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity)) 
{ 
bool isDefaultValueDefinedInModel = (edmProperty.DefaultValue != null); 
//Here I need to check whether my edmProperty is Nullable<System.DateTime> or System.DateTime, so that I can insert custom code. 
} 

請幫忙。

回答

12
if (((PrimitiveType)edmProperty.TypeUsage.EdmType). 
     PrimitiveTypeKind == PrimitiveTypeKind.DateTime && edmProperty.Nullable) 
-1

定期檢查:

if(edmproperty.GetType() == typeof(System.DateTime)){ } 

可空檢查:

if(edmproperty != null && edmproperty.GetType() == typeof(Nullable<System.DateTime>)) 
+0

非常感謝你。我以不同的方式解決了這個問題。以下是代碼。如果(((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.DateTime && edmProperty.Nullable) \t \t { – WPFProgrammer 2011-02-15 04:58:04

相關問題