Matt Stuvysant
Matt Stuvysant

Reputation: 489

How to detect classes marked with file modifier

In C# 11, the new file type modifier was introduced.

I was looking for a way to detect such non-public type using reflection, however, I have not find any "good" way to do so.

There is no Is… method that would tell if the type is scoped just to a file (in contrast to methods like IsPublic, IsNested, … and their combinations that can pinpoint whether a method is private (~ is nested) or internal (~ not public (but also not nested)), and so on).

Based on what I gathered so far, any file-scoped type:

Thus, I was able to come-up with following regular expressions to determine if a type is file-scoped or not (that is precise enough for me):

[GeneratedRegex("^<[a-zA-Z_]+>[A-F0-9]+__.*$")]
private static partial Regex FileScopeClassNameRegex()

Am I overlooking something or does the current Type model simply lack means to determine this particular scope of a class?

More precisely: Is there any systematic way to detect file-scoped classes in C#?


I played around with code snippet similar to this fiddle.

Upvotes: 2

Views: 112

Answers (0)

Related Questions