vendredi 20 août 2021

Find .cs file path from any given type in c#

The attribute [CallerFilePath] seems to do what I need to do:

How to find path to .cs file by its type in C#

...but I would like to be able to avoid having to add this new prop to every type that I want to check:

public string SourceFilePath { get; } = Helper.GetCallerFilePath();

What I have tried:

  • Adding this method at runtime does not seem to be an option.
  • Using it in a base class does not seem to work either (it returns the path of the base class).

My guess is that this is somehow feasible, because an exception is able to give you this kind of info (from any given type).


What I really want to do in my particular case is: I have a set of cs files that can be identified via their implemented interface, and I want to know their location in the file structure of the project.

So in this case I don't need to know the file location of any given type, but it would be my preferred approach if that's possible.





Aucun commentaire:

Enregistrer un commentaire