在反射的外部 dll class 中填充 List<things>

populate List<things> in reflected external dll class

请原谅我只掌握了这些术语,我在这里的 C# 知识处于边缘,需要寻求指导。

我有一个 DLL,其中包含两个 classes 和一个表单(附加 class)其中一个 classes 工作项具有 public(字符串名称,整数ID)。

// in the DLL:
public class workitems {
     public string name {get;set;}
     public int id{get;set;}
}

主力 class 有一个变量用于多个函数

// in the DLL:
public class workhorse {
    List<workitems> WorkLoad = new List<workitems>();

    public function DoThings() {   ..... stuff ...... }
}

在另一个程序中,我需要调用这个 dll(我假设是通过反射)。我试过了

 // in a separate C# script that needs to call this via reflection
 Assembly asm = Assembly.LoadFile(thedll);

但我不知道如何将工作项加载到变量中,然后使用这些工作项从 dll 中调用一个函数...我对 type/class/methodinfo/.GetType 感到困惑。 . 任何指导将不胜感激。

从必须调用 dll 文件的程序中,我需要执行如下操作:

otherdll.workload.add( stuff )

otherdll.DoThings(); (which uses the workload from that class)

该代码假定您已经有了程序集并且 Workload 是一个字段,而不是 属性:

//Get workhorse TypeInfo
var type = asm.ExportedTypes.Single(t => t.Name == "workhorse");
// Create instance of workhorse
var obj = Activator.CreateInstance(type);
// Get FieldInfo WorkLoad
var prop = type.GetField("WorkLoad");
// Get object workhorse.WorkLoad
var list = prop.GetValue(obj);
// Get MethodInfo for Add method
var method = prop.FieldType.GetMethod("Add");
// Call it with new object
method.Invoke(list, new [] { (object)new workitems()});
// Get DoThings methodinfo
var doThings = type.GetMethod("DoThings");
// call it without parameters
doThings.Invoke(obj, new object[0]);