简体   繁体   English

使用 roslyn 获取属性类型的类型参数

[英]Get type parameters of property type using roslyn

I'd like to determine the type parameter of a property represented as an IPropertySymbol with a generic type.我想确定表示为具有泛型类型的IPropertySymbol的属性的类型参数。 More precisely, I'd like to know if a property is of type int or int?.更准确地说,我想知道属性的类型是 int 还是 int?。 property.Type.OriginalDefinition gives me that it is a System.Nullable<T> but I'd like to know if it is a Nullable<int> or something else. property.Type.OriginalDefinition告诉我它是System.Nullable<T>但我想知道它是Nullable<int>还是其他东西。 Is there any way I can do this?有什么办法可以做到这一点吗?

You're looking for the TypeArguments property , which gives you exactly that.您正在寻找TypeArguments属性,它正是为您提供的。

Note that if the property is declared as Nullable<T> on a class with a generic type parameter, that will only give you a concrete type if your property symbol comes from a closed type (eg, SomeClass<int> rather than the SomeClass<> definition).请注意,如果在具有泛型类型参数的类上将属性声明为Nullable<T> ,则只有在您的属性符号来自封闭类型(例如SomeClass<int>而不是SomeClass<>定义)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM