如何通过反射

时间:2016-06-09 15:34:37

标签: c# vb.net plugins reflection

通过反射加载资源时,如何在调用 DLLAssembly.GetManifestResourceNames()

时确定我使用了正确的资源名称

该调用的结果是包含2个元素的String []。

SDKSetupTool.MainForm.resources
SDKSetupTool.Properties.Resources.resources

如果我对VB程序集进行相同的调用,它也会返回2个元素。

SDKSetupTool.MainForm.resources
SDKSetupTool.Resources.resources

Last元素始终是包含我的测试中的资源的正确位置。但是,为什么我必须解析" .resources "为了使 ResourceManager 能够加载资源

//Load the DLL Assembly from the path that was passed in
var DLLAssembly = Assembly.LoadFrom(SelectedToolPath);

//Load the resource String so that we can bootstrap our Plugin
String RawResourceLocation = DLLAssembly.GetManifestResourceNames()[1];
String ResourceLocation = RawResourceLocation.Substring(0, RawResourceLocation.LastIndexOf('.'));

//Load the resources into a Resource Manager
System.Resources.ResourceManager rman = new System.Resources.ResourceManager(ResourceLocation, DLLAssembly);

//Plugin Name
String PluginName = rman.GetString("PluginName");

//Plugin Description
String PluginDesc = rman.GetString("PluginDescription");

//Plugin Language (Currently only C# and VB.NET Supported)
 tring PluginLang = rman.GetString("PluginLanguage");

//Plugin's Entry Point (Because it is a DLL there is no Predefined Entry point. This is an alternative to that)
String PluginEntryPoint = rman.GetString("PluginEntryPoint");

//Discover our entry-point
var EntryPoint = DLLExtension.GetType(PluginEntryPoint);

//Grab the main method of the application
var MainMethod = EntryPoint.GetMethod("Main", new Type[] { });

MainMethod.Invoke(null, null);

如果我能够始终能够加载程序集的资源,我将能够根据资源中包含的字符串加载插件。

0 个答案:

没有答案