2

When trying to get constraints applied to a generic method with reflection, after calling GetGenericArguments on the MethodInfo, if I use GenericParameterAttributes I get None and GetGenericParameterConstraints returns an empty Type[] when there's only a notnull constraint.

I have tried finding differences in the generated IL and (from what I understand) there's only a NullableAttribute (from System.Runtime.ComplierServices) applied to either the generic method, its declaring type or the type parameter that has the notnull constraint depending on the definition of the class nor method.

Since the attribute's location is not always the same and I'm pretty sure there are others ways it can appear I don't think looking for this attribute is a reliable solution.

Is there something more that can tell me a type parameter has the notnull constraint using reflection?

nalka
  • 1,894
  • 11
  • 26
  • That attribute is only for Roslyn consumption, both the compiler and the static analyzers. If you're reading it through reflection, you're definitely using it wrong, it's not meant for you. Unless you're writing an analyzer of course, but then you'd be using the syntax tree instead, so again wrong. – Blindy Oct 12 '22 at 14:29
  • That said, show some code, because personally I'm having difficulty imagining a situation where reading whether an attribute is present or not is in any way a problem. – Blindy Oct 12 '22 at 14:30
  • @Blindy I didn't say its a problem, my issue here is that it doesn't look like a reliable way to determine if the `notnull` constraint is applied to a type parameter since as I explained in my question, it could be there for other reasons (for example when I was testing things, it appeared on a an other method where no type parameter had the notnull constraint, I think it was because of `#nullable enable`) – nalka Oct 12 '22 at 14:40
  • It seems to me you're confusing `NotNull` with nullable references. The former is a post-condition, it's an indication to the static analyzer that the parameter (or return) is not null at the end of the function and can be used for example in a `ThrowIfNull` function that only exits on non-null values. Again (and for the last time), show some code -- everything I've seen so far screams wrong usage of provided tools, and I can't even begin to guess at what you're trying intentionally to use wrong and what you aren't. – Blindy Oct 12 '22 at 14:45
  • @Blindy No, I know what the NotNull attribute does, I'm not talking about it, I'm talking about how to make the difference between `public void GenericMethod()` and `public void GenericMethod() where T : notnull` (here's really the only code to mention for this question) when working with reflection – nalka Oct 12 '22 at 14:53

1 Answers1

-1

how to make the difference between public void GenericMethod<T>() and public void GenericMethod<T>() where T : notnull (sic)

Finally, a piece of code.

Well a quick look does show the Nullable attribute on generic types in that case, but you have to keep in mind that generic constraints are compile-time features, they have only incidental IL code generated. Work with that if you wish, you already got there.

I will reiterate however that what you're looking for is compile-time data, and you already have a strong foundation for that: source generators. You can easily write a source generator that looks through your types and builds a public static array of whatever meta-data you wish by just parsing the syntax tree and finding your notnull constraints.

Blindy
  • 65,249
  • 10
  • 91
  • 131