从库中获取引用 C# class 库的应用程序的属性

Get properties of the application referencing a C# class library from the library

我要完成的是这个;我有一个我制作的 class 库,但我希望能够检测到哪个应用程序正在使用它,因为我有多个应用程序使用一个库,但我希望能够为一个应用程序提供某些功能并且不能让它们对任何其他应用程序可用,同时只有一个独立的 class 库。当应用程序引用或调用 class 库时,我希望能够检索的信息是;应用程序名称、版本以及是否已签名。

这可能吗?

您可以使用反射 System.Reflection.Assembly.GetEntryAssembly() 找到应用程序的入口点(例如,最初是 运行 的可执行程序集)。

Assembly caller = Assembly.GetEntryAssembly();
var name = caller.GetName();
var version = name.Version;

// etc.