1

I have a system that uses the CallerLineNumber attribute to order methods for me to retrieve them later on based on their declaration order.
This works very well, yet it does not support partial classes that are split between different files.
I do not care to support partial classes in my system, but I would like to issue an exception if someone tried to use a partial class with my system, so basically, I need a way to know if a class is declared partial, using reflection I suppose.
One idea I had was to use the CallerFilePath attribute to check that the script's attributes are all from the same file, but I was wondering if there was a simpler way.
Thank you.

Moe Baker
  • 309
  • 2
  • 8
  • Does this answer your question? [.NET reflection: how to get properties defined on partial class](https://stackoverflow.com/questions/6462959/net-reflection-how-to-get-properties-defined-on-partial-class) and [Using reflection to check if a partial method has been implemented](https://stackoverflow.com/questions/19954754/using-reflection-to-check-if-a-partial-method-has-been-implemented) –  May 22 '21 at 13:13
  • I am not aware of a simpler way. And if the `CallerLineNumber` and `CallerFilePath` attributes work for your logic, perhaps you could consider to use them both to correctly support partial classes as well? – Bart Hofland May 22 '21 at 13:34
  • 1
    _" I need a way to know if a class is declared partial"_ -- once compiled, the only indication that different parts of a class come from different source files (i.e. `partial` declarations) is the name of the source file, either set in the `[CallerFilePath]` attribute or found in the .pdb data. As far as the assembly metadata is concerned, it's all one thing. This is outlined in the duplicate, which also addresses your broader concern about ordering methods based on declaration order. – Peter Duniho May 22 '21 at 17:45

1 Answers1

0

At compile time attributes of partial class are merged. So, there is no way to check if class is partial via reflection (as no way also to see names of local variables, commentaries and preprocessor directives).

kogonidze
  • 129
  • 1
  • 11