How to get string type for types with zero value

I am creating a T4 template using the properties of my data class generated from my dbml files. To get the property type of classes, I use item.PropertyType.Name , the problem is that with types with a null value, it returns Nullable `` 1 , is there a way to get Nullable for example, or int? `?

+4
source share
3 answers

GetGenericArguments is the method you want.

 if (item.PropertyType.IsGenericType) { if (item.PropertyType.GetGenericType() == typeof(Nullable<>)) { var valueType = item.PropertyType.GetGenericArguments()[0]; } } 

Secondly, however, Darren's answer is much simpler in this case, as it will return null when you pass a type with a null value.

+7
source

int? === Nullable<int> . they are one in one. If you want to know which type is nullable, you can use the Nullable.GetUnderlyingType(typeof(int?)) Method to get the type (in this case int )

Nullable.GetUnderlyingType

+11
source

Take a look at this other question . Marc Gravell's answer will tell you how to get the type of general arguments.

0
source

Source: https://habr.com/ru/post/1301391/


All Articles