简体   繁体   English

如何将项目类型转换为共享类库类型

[英]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 . 我不希望ProjectTypeSharedType继承,有两个原因(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.

相关问题 如何在.NET核心类库中引用Visual Studio共享项目 - How do I reference a Visual Studio Shared Project in a .NET Core Class Library 如何在类库项目类型中使用ConfigurationManager类 - How to use ConfigurationManager Class in a Class Library Project type 如何在Visual Studio中使用类库的输出类型调试项目? - How to debug a project with Output Type of Class Library in Visual Studio? 如何从类型转换为通用? - How do I convert from type to generic? 如何转换为IList类型? - How do I convert to the type IList? 如何通过类库项目中的类获取主项目中类的类型 - How to get the Type of a class in Main Project through a class in Class Library Project 在.net类库中检测调用项目的类型 - Detect calling project type in a .net class library 错误:具有类库的输出类型的项目 - Error: A project with an out put type of class library 服务器和客户端之间共享的库 - 不能隐式转换类型 - Library shared between server and client - cannot implicitly convert type 如何在返回抽象 class 类型的方法中将字符串值转换为可返回值? - How do I convert a string value to be returnable in a method that returns an abstract class type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM