[英]How do I convert a project type to shared class library type
(C# code samples are at the bottom of the question.) (C#代码示例在问题的底部。)
I have a class library shared between a number of solutions, with a type called SharedType
. 我有一个在许多解决方案之间共享的类库,其类型为SharedType
。 I have a method in the class library which processes a collection of this type: 我在类库中有一个方法可以处理这种类型的集合:
Sub ProcessSharedTypeEnumerable(src As IEnumerable(Of SharedType))
'do something here
End Sub
Each solution has its own specific type that should map/convert to the SharedType
: 每个解决方案都有其自己的特定类型,该类型应映射/转换为SharedType
:
Namespace Project1
Class ProjectType
End Class
End Namespace
How can I call ProcessSharedTypeEnumerable
without having to convert ProjectType
to SharedType
at the call site? 如何在不调用站点的情况下将ProjectType
转换为SharedType
情况下调用ProcessSharedTypeEnumerable
?
Dim lst As New List(Of ProjectType)
'populate list
'instead of this:
ProcessSharedTypeEnumerable(lst.Select(Function(x) New SharedType With { 'populate members here' })
'use this:
ProcessSharedTypeEnumerable(lst)
I know I can have the project type implement an interface from the class library, which has a method that maps to the shared type 我知道我可以让项目类型从类库中实现接口,该类库具有映射到共享类型的方法
Public Interface ISharedTypeMappable
Function ToSharedType() As SharedType
End Interface
Then the ProcessSharedTypeEnumerable
method can take IEnumerable(Of ISharedTypeMappable)
and I can call .Select(Function(x) x.ToSharedType)
within ProcessSharedTypeEnumerable
to get IEnumerable(Of SharedType)
. 然后, ProcessSharedTypeEnumerable
方法可以采用IEnumerable(Of ISharedTypeMappable)
并且我可以在ProcessSharedTypeEnumerable
调用.Select(Function(x) x.ToSharedType)
以获取IEnumerable(Of SharedType)
。
Is there a "more correct" way to do this? 是否有“更正确”的方式来做到这一点? Generic covariance and implicit conversion? 通用协方差和隐式转换? Some .NET built-in conversion mechanism? .NET内置的转换机制?
C# code samples C#代码示例
Class library method: 类库方法:
static class Utils { //I know it's an awful name ...
static void ProcessSharedTypeEnumerable(IEnumerable<SharedType> src) {
//do something here
}
}
Project code: 项目代码:
var lst = new List<ProjectType>();
//populate list
//instead of this:
Utils.ProcessSharedTypeEnumerable(lst.Select(x=>new SharedType { /* populate properties here */}));
//do this
Utils.ProcessSharedTypeEnumerable(lst);
Update 更新
I don't want to have ProjectType
inherit from SharedType
for two reasons (1) ProjectType
already inherits from a different base type, and (2) SharedType
may have members that should not be on ProjectType
. 我不希望ProjectType
从SharedType
继承,有两个原因(1) ProjectType
已经从其他基本类型继承,并且(2) SharedType
可能具有不应属于ProjectType
成员。
Inheritance? 遗产?
Sub Main()
Dim lst As New List(Of ProjectType) From {
New ProjectType With {.s = "1"},
New ProjectType With {.s = "2"},
New ProjectType With {.s = "3"}
}
SharedType.ProcessSharedTypeEnumerable(lst)
End Sub
Shared Sub ProcessSharedTypeEnumerable(src As IEnumerable(Of SharedType))
For Each st As SharedType In src
MsgBox(st.s)
Next
End Sub
Class SharedType
Property s As String
End Class
Class ProjectType
Inherits SharedType
End Class
EDIT: This is now a community wiki answer 编辑:这现在是社区维基的答案
As requested, here's my attempt at implementing Operator Widening CType
. 根据要求,这是我尝试实现Operator Widening CType
尝试。 It does not work, though. 但是,它不起作用。 Maybe someone reading this can work it out. 也许有人读这个可以解决。
Module Module1
Sub Main()
Dim lst As New List(Of ProjectType) From {
New ProjectType With {.t = "1"},
New ProjectType With {.t = "2"},
New ProjectType With {.t = "3"}
}
MsgBox(CType(lst(0), SharedType).s) 'This works'
ProcessSharedTypeEnumerable(lst) 'This does not :-( '
ProcessSharedTypeEnumerable(lst.Cast(Of SharedType).ToList) 'Neither does this :-( '
End Sub
Sub ProcessSharedTypeEnumerable(src As IEnumerable(Of SharedType))
For Each st As SharedType In src
MsgBox(st.s)
Next st
End Sub
Class SharedType
Property s As String
End Class
Class ProjectType
Property t As String
Public Shared Widening Operator CType(p As ProjectType) As SharedType
Return New SharedType With {.s = p.t}
End Operator
End Class
End Module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.