C# 将自定义类型发送到DLL

C# 将自定义类型发送到DLL,c#,C#,我创建了一个自定义类型(StampDates),它由一个InDate和一个OutDate组成 这些对象的列表被发送到一个对其进行计算的dll,并返回一个值。目前,这只是一组日期,但我将发送填充列表,dll将返回所有日期的总和 dll中的方法需要类型为的列表: 如何解决这个问题?我建议在这里使用接口: 动态链接库: //在dll中,我们不关心实际的实现。 //为了执行我们的任务,我们只需要它有两个属性: 公共接口日期 { //实现必须具有这些读/写属性 DateTime InDateTime{ge

我创建了一个自定义类型(StampDates),它由一个InDate和一个OutDate组成 这些对象的列表被发送到一个对其进行计算的dll,并返回一个值。目前,这只是一组日期,但我将发送填充列表,dll将返回所有日期的总和

dll中的方法需要类型为
的列表:


如何解决这个问题?

我建议在这里使用接口:

动态链接库:

//在dll中,我们不关心实际的实现。
//为了执行我们的任务,我们只需要它有两个属性:
公共接口日期
{
//实现必须具有这些读/写属性
DateTime InDateTime{get;set;}//我们甚至可以将这些设置为只读。。。
DateTime OutDateTime{get;set;}
}
//在包含该方法的类中。。。
公共整数天数(IEnumerable dates)
{
//除了dotnet framework(对于IEnumerable),实现没有依赖项
}
在消费应用程序中:

// Implementation of the IStampDates interface
class StampDates : IStampDates
{
    public DateTime InDateTime {get; set;}
    public DateTime OutDateTime {get; set;}
}

// Consuming part somewhere in a method, perhaps
List<IStampDates> stampDateList = new List<IStampDates>();
// ... populate list
stampDateList.Add( new StampDates() { InDateTime = ..., OutDatTime = ... });

// Call Dll
int days = dll.NumDays(stampDateList);
// assuming "dll" is an instance of the class that contains `NumDays`
//IStampDates接口的实现
类StampDates:IStampDates
{
public DateTime InDateTime{get;set;}
public DateTime OutDateTime{get;set;}
}
//可能是在某个方法中的某个地方消耗部分
List stampDateList=新列表();
// ... 填充列表
添加(新的StampDates(){InDateTime=…,outdatetime=…});
//调用Dll
int days=dll.NumDays(stampDateList);
//假设“dll”是包含`NumDays'的类的实例`
因此,您的应用程序依赖于dll。那很好。但是dll在自身或框架之外没有依赖性(这是不可避免的)。 这意味着:如果您在另一个应用程序中使用此dll,
StampDates
的实际实现可能完全不同,只要它实现了
IStampDates
的dll接口


要点是:如果您在这里使用接口,那么它传递的实际运行时类型并不重要(给定,它实现接口)。因此,它可以在应用程序、dll(应用程序应该使用它,而不是创建(代码)副本)中定义,甚至可以在第三个共享依赖项中定义。这一切都取决于您如何构建解决方案。

为什么不使用Dll的
StampDates
?您在哪里申报天数?不应该编译,imho。我还建议对接口进行编码(即,
IEnumerable
而不是
List
)“我还需要在那里定义自定义类型”-如果您定义一个新类型,那么您有两种不同的类型。不要定义新的,而是使用相同的。(如果您不希望其他层引用DLL,可以在引用DLL的层中从一种类型转换为另一种类型。)请参见,即使
Form.StampDates
DLL.StampDates
具有相同的名称并且实现相同,对于编译器来说,它们是两种不同的类型。所以,这完全是意料之中的。^^我个人甚至根本不会在Dll中使用
StampDates
实现。我有一个接口
IStampDates
,该方法的参数为
IEnumerable
。您定义实际实现的地方在另一页上。感谢Fildor提供的示例。我能够让它启动并运行,我将努力在头脑中理解它!如果你不熟悉这个概念,慢慢来。你用得越多,它就会变得越容易。
public class StampDates
{
    private DateTime inDateTime;

    public DateTime InDateTime
    {
        get { return inDateTime; }
        set { inDateTime = value; }
    }

    private DateTime outDateTime;

    public DateTime OutDateTime
    {
        get { return outDateTime; }
        set { outDateTime = value; }
    }
}
// In the dll we do not care about the actual implementation.
// To perform our task, we just need this to have two properties:
public interface IStampDates 
{
    // Implementations must have these read/write properties
    DateTime InDateTime {get; set;}  // We even could make those readonly ...
    DateTime OutDateTime {get; set;}
}

// in the class that contains the method ...
public int NumDays(IEnumerable<IStampDates> dates)
{
      // Implementation has no dependency other than dotnet framework (for IEnumerable)
}
// Implementation of the IStampDates interface
class StampDates : IStampDates
{
    public DateTime InDateTime {get; set;}
    public DateTime OutDateTime {get; set;}
}

// Consuming part somewhere in a method, perhaps
List<IStampDates> stampDateList = new List<IStampDates>();
// ... populate list
stampDateList.Add( new StampDates() { InDateTime = ..., OutDatTime = ... });

// Call Dll
int days = dll.NumDays(stampDateList);
// assuming "dll" is an instance of the class that contains `NumDays`