[英]c# how to make instantiation more abstract?
I am currently writing a VSTO for Excel but this would apply to other applications, too. 我目前正在为Excel编写VSTO,但这也适用于其他应用程序。 The app is for a number of sites, so I am writing core logic and site-specific logic.
该应用程序可用于许多站点,因此我正在编写核心逻辑和特定于站点的逻辑。 I am trying to keep all site-specific logic in a single subfolder to make it easier to exchange between sites.
我试图将所有特定于站点的逻辑保留在单个子文件夹中,以使站点之间的交换更加容易。
VSTOs have a class called ThisAddIn.cs
. VSTO有一个名为
ThisAddIn.cs
的类。 It instantiates a class called AddInUtilities
. 它实例化一个名为
AddInUtilities
的类。 Mine looks like this: 我的看起来像这样:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{
public void ImportData()
{
var activeWorksheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;
if (activeWorksheet == null) return;
var loadCsv = new CsvImporter();
if (loadCsv.IsNotCancelled) loadCsv.AndImportIndexDataInto(activeWorksheet);
}
}
My CsvImporter
class is site-specific. 我的
CsvImporter
类是特定于站点的。 It is based on an abstract class, which in turn is based on an interface, which allowed me to move everything that is core logic in the importer away from the site-specific subfolder. 它基于抽象类,而抽象类又基于接口,这使我可以将导入器中作为核心逻辑的所有内容从特定于站点的子文件夹中移开。
What I can't get my head around is how to make the instantiation of my class 我无法理解的是如何使我的课程实例化
var loadCsv = new CsvImporter();
abstract? 抽象? I obviously cannot instantiate the interface or abstract class.
我显然无法实例化接口或抽象类。 I could move this class into the site-specific code, but then my
ThisAddIn.cs
, which refers to this class will need to refer to site-specific code (or at least use the namespace of that subfolder). 我可以将此类移至特定于站点的代码中,但是引用该类的
ThisAddIn.cs
将需要引用特定于站点的代码(或至少使用该子文件夹的名称空间)。
The only way around that, that I can think of, would be to move the files physically into the subfolder but leave the namespace as is. 我能想到的解决此问题的唯一方法是将文件物理地移到子文件夹中,但保留名称空间不变。 Is there a better way?
有没有更好的办法? I feel I am missing something.
我觉得我缺少什么。
I would recommend to make loadCsv a class property and push the needed object from the site specific code: 我建议将loadCsv设为一个类属性,并从特定于站点的代码中推送所需的对象:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{
ICsvImporter CsvImporter = null;
public void ImportData()
{
var activeWorksheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;
if (activeWorksheet == null) return;
if (CsvImporter != null && CsvImporter.IsNotCancelled)
{
CsvImporter.AndImportIndexDataInto(activeWorksheet);
}
}
}
public class SiteSpecificClass
{
public void SiteImportMethod()
{
var addinUtility = new AddInUtilities();
addinUtility.CsvImporter = new CsvImporter();
addinUtility.ImportData();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.