开发者

Get source filename or per file identifier using Reflection?

开发者 https://www.devze.com 2022-12-15 22:52 出处:网络
I looked and it doesnt seem possible but it might be. Using reflection i would like to know if two classes were compiled/defined in the same source file? I would like to use a class as a configuration

I looked and it doesnt seem possible but it might be. Using reflection i would like to know if two classes were compiled/defined in the same source file? I would like to use a class as a configuration file and found a way to do it per namespace but would like to开发者_StackOverflow use a per file solution


At first I thought this is not possible, but this guy came up with a scheme that does not require deploying the PDB files, but compiled a database from them in order to get this info.

I would say that you do have to have the PDB files, or at least some of the data in them in order to do this.


There is no way to do this via reflection if you haven't decorated your classes in some way to indicate their source file. To do so, you would need to write a simple attribute, let's call it SourceFileAttribute, then you would need some way to ensure your decoration was up-to-date on each class. You could do that entirely by hand, use source control to populate the filename portion, or have a tool do it during compilation that would insert both the attribute and the filename.

Once you have that attribute, you can look for it using reflection to perform your checks.

An alternative might be to use PDB information although I would expect that to be slower. The link provided in Oded's answer provides a reasonable compromise to avoid interpreting the PDBs at runtime (although I disagree about the suggestion not to deploy PDBs as a matter of due process).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号