简体   繁体   English

使用通用返回类型不工作的重载静态方法

[英]Overloaded static methods using Generic Return Types Not Working

I have the following 2 overloaded staic methods defined in my static Class: 我在静态类中定义了以下2个重载的staic方法:

private static Dictionary<int, PartitionDefinition> GetNewPartitionDefinitions (string  measureGroup, int rangeThreshold)
    {
         do something;
}
private static Dictionary<int, PartitionDefinition> GetNewPartitionDefinitions(DateRange startDateRange)
{
do something else;
}

The behavior I'm getting is which ever method is listed first in the code is the one that is recognized during design-time. 我得到的行为是代码中首先列出的方法是在设计时识别的方法。 The subsequent method name in the chain gets greyed out and I if I hover over it I receive the message 'Method is never used'. 链中的后续方法名称变为灰色,如果我将鼠标悬停在其上,则会收到消息“方法永远不会被使用”。

I have also noted that if I attempt to call the method, intellisense only detects the first one appearing in the code, like the other doesn't exist. 我还注意到,如果我尝试调用该方法,intellisense只检测出现在代码中的第一个,就像另一个不存在一样。

Researching the problem I "believe" I can eliminate the condition of Type Erasure as being the issue but I'm not 100% absolutely certain of this. 研究问题我“相信”我可以消除类型擦除的条件作为问题,但我并非100%绝对肯定这一点。

I do know that the following code would constitute valid for Type Erasure using Generics: 我知道以下代码对于使用泛型的类型擦除有效:

public class A {
 public static MethodOne  (List<String> param) {}
 public static MethodOne  (Dictionary<String,String> param) {}
}

But in this case, the generics are being passed as parameters so I can see how this would cause a problem. 但在这种情况下,泛型作为参数传递,所以我可以看到这将导致问题。 In my instance, only the return types are identical Generic types but one would think that having the same method name and clearly having different non-generic parameter types and number should work. 在我的实例中,只有返回类型是相同的泛型类型,但有人会认为具有相同的方法名称并且明确具有不同的非泛型参数类型和数量应该有效。

Could someone please shed a bit of light on this? 有人可以对此有所了解吗? I have looked at similar article postings here, but none of them seem to describe the precise problem I am seeing. 我在这里查看了类似的文章帖子,但它们似乎都没有描述我看到的确切问题。

I am not new to programming but I don't claim to know everything and this one is a first for me. 我不是编程的新手,但我不认识一切,这对我来说是第一次。

The Message you receive might be because you are not using the methode in your code, the issue to overload static methods is actually allowed and shouldnt be a problem or cause any compiler error, you may want to check your configuration for the warnings and hints in VS. 您收到的消息可能是因为您没有在代码中使用该方法,实际上允许重载静态方法的问题并且不应该是一个问题或导致任何编译器错误,您可能需要检查配置中的警告和提示VS.

I just tested it using this code: 我刚用这段代码测试了它:

public static class TestClass
    {
        public static string stat(string measureGroup, int rangeThreshold)
        {
            return "st1";
        }

        public static string stat(int startDateRange)
        {
            return "st2";
        }

        private testmethode()
        {
           // string h = TestClass.stat(.... at this point i get both variants offered)
        }

    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM