简体   繁体   English

我需要一个解决方案来进行类型参数重载

[英]i need a solution to do type parameter overloading

I need type parameter overloading as such: 我需要这样的类型参数重载:

Public Class Map(Of TKey, TValue)

Public Class Map(Of TKey As IEquatable(Of TKey), TValue)

so that i can New Map(Of Human) and the compiler will automatically match it to Public Class Map(Of TKey, TValue) and if i new Map(Of String) the compiler will automatically match it to Public Class Map(Of TKey As IEquatable(Of TKey), TValue) (since String is an IEquatable(Of String)) 这样我就可以New Map(Of Human) ,并且编译器会自动将其与Public Class Map(Of TKey, TValue)匹配;如果我new Map(Of String)则编译器会自动将其与Public Class Map(Of TKey As IEquatable(Of TKey), TValue) (因为String是IEquatable(Of String))

Right now my solution is to give the class different names as such: 现在,我的解决方案是为类提供不同的名称,例如:

Public Class Map(Of TKey, TValue)

Public Class EqMap(Of TKey As IEquatable(Of TKey), TValue)

But I'm looking for a better solution 但我正在寻找更好的解决方案

Sorry I don't speak VB... but in c# (which you have in your tags) what you want is 抱歉,我不会说VB ...但是在C#中(您的标签中有),您想要的是

Map<TKey,TValue>
{
    // implementation
}

and

Map<Tkey,TValue>
   where TKey: IEquatable
{

}

unfortunately this isn't supported because constraints are not part of the signature you will have to provide different signatures and using different names (or possibly namespaces) is the cleanest solution IMO. 不幸的是,不支持此方法,因为约束不是签名的一部分,您将必须提供不同的签名,并且使用不同的名称(或可能使用名称空间)是IMO的最简洁解决方案。

There is no solution, better than what you propose. 没有比您建议的更好的解决方案。 Personally I would prefer EquatableMap to EqMap , EquatableMap could inherit Map if the is relationship and reuse was beneficial. 我个人更喜欢EquatableMapEqMap ,如果is关系和重用是有益的,则EquatableMap可以继承Map

.Net does not support class overloading, who knows, maybe in .Net 5.0. .Net不支持类重载,谁知道呢,也许在.Net 5.0中。 It would be useful. 这将是有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要一个解决方案来创建 xUnit 测试吗? - Do I need a solution to create xUnit tests? 我需要为嵌套搜索创建类型吗? - Do I need to create a Type for Nest Search? 为什么我需要 Content/Type 变量? - Why do I need Content/Type variable? 如何使用类型列表的参数创建泛型类型的实例 - How do I create an instance of a generic type with a parameter of type list 我是否需要释放用作函数参数的托管BSTR - Do I need to free a managed BSTR used as a function parameter C# - 为什么我需要初始化[Out]参数 - C# - Why do I need to initialize an [Out] parameter 我需要通过非常严格的参数来计算学生人数,我不知道该怎么做 - I need to count students by very hard parameter, and idk how to do it 更改web.config中的任何内容后是否需要重建解决方案? - Do i need to rebuild my solution after changing anything in web.config? 我如何在vb.net中查看rgroups转发器内的asp:textbox需要解决方案 - how do i view the asp:textbox inside rgroups repeater need solution in vb.net 给定参数的ParameterInfo时,如何检查服务方法的参数类型是否为输出类型? - How do I check if a service method's parameter type is an output type given the parameter's ParameterInfo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM