我在这里进行了大量的搜索和搜索,但是无法在从DLL返回之前弄清楚如何从动态调用的DLL中获取进度消息。可以说DLL已完成10%...我想显示进度。我习惯于仅显示呼叫返回的内容,但在这种情况下,我需要进度。这是我调用DLL的代码(C#,WPF项目)。只是好奇如何在退货之前退回邮件...
string pathAndDLLFile = Path.GetFullPath(DLLName + ".DLL");
Assembly a = Assembly.LoadFile(pathAndDLLFile);
string typeField = DLLNamespace + "." + DLLClass;
Type myType = a.GetType(typeField);
MethodInfo testMethod = myType.GetMethod(DLLMethod);
object obj = Activator.CreateInstance(myType);
object c = testMethod.Invoke(obj, new Object[] { nodeChild });
这是我的DLL ...
using System.Xml;
namespace InitialTest2
{
public class Class1
{
public int test(XmlNode xmlTest)
{
int testCount = 0;
int progress = 0;
foreach (XmlAttribute attributeChild in xmlTest.Attributes)
{
if (attributeChild.Name != "name" && attributeChild.Name != "testNumber" && attributeChild.Name != "Estimate" && !attributeChild.Name.Contains("DLL"))
{
if ((attributeChild.Name.Contains("MinLimit")) || (attributeChild.Name.Contains("MaxLimit")) || (attributeChild.Name.Contains("Unit")))
{
// not the attribute value
}
else
{
testCount ++;
}
}
progress = progress + 1;
// ToDo: report progress back to the main ap
}
return testCount;
}
public int test3(XmlNode xmlTest)
{
return 3;
}
}
public class Class2
{
public int test2(XmlNode xmlTest)
{
return 2;
}
}
}
答案 0 :(得分:1)
这与动态调用无关,但事实是导览test
方法返回单个int
值,而没有其他值。
这种方法无法向调用者报告或返回int
值以外的任何值。您期望如何以及在何处获取局部变量progress
的当前值?在foreach
循环完成之前,您不会从方法中返回任何内容。
如果您想从同步方法返回某种进度或至少几个值,则可以将其返回类型更改为IEnumerable<int>
并遍历返回值,例如:
public IEnumerable<int> test(XmlNode xmlTest)
{
int testCount = 0;
int progress = 0;
foreach (XmlAttribute attributeChild in xmlTest.Attributes)
{
if (attributeChild.Name != "name" && attributeChild.Name != "testNumber" && attributeChild.Name != "Estimate" && !attributeChild.Name.Contains("DLL"))
{
if ((attributeChild.Name.Contains("MinLimit")) || (attributeChild.Name.Contains("MaxLimit")) || (attributeChild.Name.Contains("Unit")))
{
// not the attribute value
}
else
{
testCount++;
}
}
yield return progress + 1;
// ToDo: report progress back to the main ap
}
yield return testCount;
}
用法:
IEnumerable<int> c = testMethod.Invoke(obj, new Object[] { nodeChild }) as IEnumerable<int>;
if (c != null)
{
int count;
foreach (var progress in c)
{
Console.WriteLine(progress);
count = progress;
}
Console.WriteLine(count);
}