如何从动态加载的程序集中设置静态类中的属性值?

问题描述 投票:0回答:0

在正在加载的 DLL 中,存在这些属性(以及其他属性):

public static partial class ThorUpdater
{
  public static string UpdaterName { get; set; }
  public static string Description { get; set; }
}

我需要从加载 DLL 的代码中设置这些属性的值。这是代码:

Assembly assembly = Assembly.LoadFrom(UpdaterFilePath);
Type UpdaterType = assembly.GetType("ThorUpdaters.ThorUpdater");

我可以这样得到这些属性的值:

UpdaterType.GetProperty("UpdaterName").GetValue(null, null);
UpdaterType.GetProperty("Description").GetValue(null, null);

但我似乎无法弄清楚如何设置这些值。在尽可能多地研究之后,我尝试了下面的两个例子。我发现的所有示例都涉及在内部程序集中使用反射,但找不到与外部程序集无关的内容。

UpdaterType.GetProperty("UpdaterName").SetValue(null, "New Value");
UpdaterType.GetProperty("Description").SetValue(assembly, "NewValue");
c# dll .net-assembly system.reflection
© www.soinside.com 2019 - 2024. All rights reserved.