我看到了一些可能对我有帮助的例子,但我没有那么多时间去探索它们,因为我今天才发现我的老板必须比计划提前一周进行演示,并且我想添加这个新功能。我会尽量保持简短。
好吧,这就像我第 10 次尝试更正它以澄清问题,希望是这样。这是一个应用程序。数据行需要显示在 DataGridView 中(完成)。某些行根据报告(完成)以不同方式突出显示。大多数报告都有自己的 SQL 文件,并在运行时从 INI 文件中实现(已完成)。但是,有些报表需要调用一个函数。该应用程序正在使用 SQLite 数据库。我希望 DLL 是报告,所有格式相同,并且所有 DLL 都返回一个 ReportRecord 列表。 ReportRecord 是在我的主应用程序中定义的一个类,但我也会在创建它们时在每个 DLL 中定义它。我想实例化 DLL,调用它的“GetRecords”函数,并在我的主应用程序中使用它。这是一些伪代码。如果你们能告诉我这是否可行,或者给我一个更好的方法,我将不胜感激。
伪代码
foreach (string str in System.IO.Directory.GetFiles("C:\\ReportDlls", "*.dll"))
{
//Instantiate DLL e.g. newReport
//_lstReportRecords.AddRange(newReport.GetReportRecords());
}
有什么办法吗?
目前,我有以下补充,直到我发现这一点:
private void RefreshReports(string strReportTitle)
{
_lstReportRecords = _lstReportRecords.Where(rr => rr.Description != strReportTitle).ToList<ReportRecord>();
string strColumn = iniFile.GetString(strReportTitle, "Column", "");
if (strColumn != null)
{
_lstReportRecords.AddRange(_dataController.BuildReportList(strColumn, strReportTitle, GetReportSQL(strReportTitle)));
}
else
{
switch (strReportTitle)
{
case "Improper Indenture":
_lstReportRecords.AddRange(_dataController.ImproperIndenture());
break;
case "Skipping Figure":
_lstReportRecords.AddRange(_dataController.SkippingFigure());
break;
default: break;
}
}
FormatCells();
}
谢谢大家
编辑:对不起各位,看着那些东西真的让我觉得自己很愚蠢。就像,我的脑子一片空白,无法集中注意力。 :) 你们提供的可能是最好的方法,但由于我必须在星期二之前准备好一个高质量的演示,并且在那之前不应该再添加任何需要功能的报告,所以我将保持开放状态。一旦我的老板出城进行演示,我就会着手实现。但是现在,除非我看到一个非常非常(对于 2 岁的 child )直截了当的例子,否则这个问题将没有答案。
最佳答案
您可以简单地创建一个实现以下接口(interface)的 C# 库项目,并将二进制文件存储在数据库或文件系统中。然后,您可以从原始程序集字节/文件路径加载程序集并实例化一个对象。通过反射,您还可以直接调用构造函数,但我更喜欢使用工厂模式来完成此类任务。
public interface IReportModule
{
}
public interface IReportModuleFactory
{
IReportModule Create();
}
private static IReportModule CreateReportModuleFromRawAssemby(byte[] rawAssembly)
{
var reportModule = Assembly.Load(rawAssembly);
var factoryType = reportModule.GetExportedTypes()
.FirstOrDefault(x => x.IsAssignableFrom(typeof(IReportModuleFactory)));
if (factoryType != null)
{
var reportModuleFactory = (IReportModuleFactory)
reportModule.CreateInstance(factoryType.FullName);
return reportModuleFactory.Create();
}
else
throw new NotImplementedException("rawAssembly does not implement IReportModuleFactory");
}
关于C#:如何动态加载/实例化 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3679812/