[英]send a custom type to DLL
I have created a custom type (StampDates) that consist of an InDate and an OutDate A List of these objects is sent to a dll that does some math on it, and return a value.For now, this is just 1 set of dates, but I will send filled Lists, and the dll will return a sum of them all.我创建了一个自定义类型 (StampDates),它包含一个 InDate 和一个 OutDate 这些对象的列表被发送到一个 dll,它会对其进行一些数学运算,并返回一个值。现在,这只是一组日期,但我会发送填充的列表,dll 将返回它们的总和。
The method in the dll expects a list of the type <StampDates>
: dll 中的方法需要
<StampDates>
类型的列表:
public int NumDays(List<StampDates> dates)
{
foreach (StampDates datums in dates)
{
DateTime indatum = datums.InDateTime;
DateTime uitdatum = datums.OutDateTime;
if ((DateTime.Now.Date - indatum).TotalDays <= 180)
{
Days = (uitdatum-indatum ).Days;
}
}
return Days;
}
This works fine, but because I need to create that list in my external form, I need to define the custom type there also.这工作正常,但因为我需要在我的外部表单中创建该列表,我还需要在那里定义自定义类型。 This gives a type conversion error between the Form.StampDates and the DLL.Stampdates
这会在 Form.StampDates 和 DLL.Stampdates 之间产生类型转换错误
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; }
}
}
How can I solve this?我该如何解决这个问题?
I would recommend the usage of interfaces here:我会在这里推荐使用接口:
DLL:动态链接库:
// 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)
}
In the consuming App:在消费应用程序中:
// 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`
So, your App has a dependency to the dll.因此,您的应用程序依赖于 dll。 That's fine.
没关系。 But your dll has no dependency outside itself or the framework (which is kind of unavoidable).
但是您的 dll 本身或框架之外没有任何依赖性(这是不可避免的)。 That means: if you use this dll in another App, the actual implementation of
StampDates
can be totally different, as long as it implements the Dll's interface of IStampDates
.这意味着:如果你在另一个 App 中使用这个 dll,
StampDates
的实际实现可能完全不同,只要它实现了IStampDates
的 Dll 接口。
The point of this being: If you use an interface here, it doesn't matter what the actual runtime type it is you are passing (given, it implements the interface).重点是:如果您在这里使用接口,则传递的实际运行时类型并不重要(假设它实现了接口)。 So it could be defined in the App, in the dll (the app should then use it, not create a (code)copy) or even in a third shared dependency.
所以它可以在应用程序中定义,在 dll 中(应用程序应该使用它,而不是创建(代码)副本)甚至在第三个共享依赖项中。 All depends on how you structure your solution.
一切都取决于您如何构建解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.