C# - 取决于预期类型的​​方法行为

时间:2022-02-27 15:38:37

Is it possible to write in C# method in such a way that when I write

是否有可能以我写的方式用C#方法编写

 String contestId = getParameter("contestId")

i get contestId in string, but when I write:

我在string中获得了contestId,但是当我写道:

 int contestId = getParameter("contestId")

i get contestId parsed to integer?

我把contestId解析为整数?

This is only simple example showing what i try to achieve.

这只是一个简单的例子,展示了我试图实现的目标。

5 个解决方案

#1


Nope it's not possible to overload methods solely based on their return type. You could, however, introduce a generic parameter:

不,不可能仅根据返回类型重载方法。但是,您可以引入一个通用参数:

T getParameter<T>(string input) {
    // ... do stuff based on T ...
}

And if you were using C# 3.0 you could use this method as:

如果您使用的是C#3.0,则可以使用此方法:

var str = getParameter<string>("contestid");
var integer = getParameter<int>("contestid");

thus saying the actual type only once.

因此只说实际类型一次。

#2


One thing you could do is return a separate object, which has implicit conversion operators to both int and string. That would get fairly close to the behavior you're asking for.

您可以做的一件事是返回一个单独的对象,它具有int和string的隐式转换运算符。这将非常接近你要求的行为。

I wouldn't do that in practice though. Implicit conversions generally cause more trouble than they're worth.

但我不会在练习中这样做。隐含的转换通常会带来比它们更值得的麻烦。

Instead, add a generic parameter, like Mehrdad showed:

相反,添加一个通用参数,如Mehrdad所示:

var str = getParameter<string>("contestid");
var integer = getParameter<int>("contestid");

#3


I prefer this approach, it reads nicely.

我更喜欢这种方法,它读得很好。

Public Class ResultProxy
{
  Private Object _Obj
  Public ResultProxy(Object O)
  { _Obj = O; }

  Public T As<T>()
  { return (T)_Obj; }
}

...

Public ResultProxy getParameter("contestId")
{
// your method's code
   return new ResultProxy(YourPersonalFavorateReturnType);
}

...

String s = getParameter("contestId").As<String>();

#4


Firstly the answer is no as many people have mentioned. Why? Do you have to assign the result of a method to something? For example can you have

首先,答案是没有人提到的那么多。为什么?你必须将方法的结果分配给某些东西吗?比如你能拥有

int getValue()
{
  return 4;
}

getValue();

The answer is yes, it can, so there is no way for the compiler to know which method you intend to call by its return type.

答案是肯定的,它可以,因此编译器无法通过其返回类型知道您打算调用哪个方法。

Personal opinion here, but I would suggest something along the lines of

个人意见,但我会提出一些建议

public string getContestIdAsString(string ConetestId);

public int getContestIdAsInt(string ContestId);

Very obvious what each one is doing and you get around your problem. Unless there is something that I am missing.

很明显每个人都在做什么,你解决了你的问题。除非有我遗失的东西。

#5


public T GetParameter<T>(string parameterName)
{
   //Do work
   return value
}

string contestId = getParameter<string>("contestId")
int contestId = getParameter<int>("contestId")

This is an example of your best bet.

这是您最好的选择的一个例子。

#1


Nope it's not possible to overload methods solely based on their return type. You could, however, introduce a generic parameter:

不,不可能仅根据返回类型重载方法。但是,您可以引入一个通用参数:

T getParameter<T>(string input) {
    // ... do stuff based on T ...
}

And if you were using C# 3.0 you could use this method as:

如果您使用的是C#3.0,则可以使用此方法:

var str = getParameter<string>("contestid");
var integer = getParameter<int>("contestid");

thus saying the actual type only once.

因此只说实际类型一次。

#2


One thing you could do is return a separate object, which has implicit conversion operators to both int and string. That would get fairly close to the behavior you're asking for.

您可以做的一件事是返回一个单独的对象,它具有int和string的隐式转换运算符。这将非常接近你要求的行为。

I wouldn't do that in practice though. Implicit conversions generally cause more trouble than they're worth.

但我不会在练习中这样做。隐含的转换通常会带来比它们更值得的麻烦。

Instead, add a generic parameter, like Mehrdad showed:

相反,添加一个通用参数,如Mehrdad所示:

var str = getParameter<string>("contestid");
var integer = getParameter<int>("contestid");

#3


I prefer this approach, it reads nicely.

我更喜欢这种方法,它读得很好。

Public Class ResultProxy
{
  Private Object _Obj
  Public ResultProxy(Object O)
  { _Obj = O; }

  Public T As<T>()
  { return (T)_Obj; }
}

...

Public ResultProxy getParameter("contestId")
{
// your method's code
   return new ResultProxy(YourPersonalFavorateReturnType);
}

...

String s = getParameter("contestId").As<String>();

#4


Firstly the answer is no as many people have mentioned. Why? Do you have to assign the result of a method to something? For example can you have

首先,答案是没有人提到的那么多。为什么?你必须将方法的结果分配给某些东西吗?比如你能拥有

int getValue()
{
  return 4;
}

getValue();

The answer is yes, it can, so there is no way for the compiler to know which method you intend to call by its return type.

答案是肯定的,它可以,因此编译器无法通过其返回类型知道您打算调用哪个方法。

Personal opinion here, but I would suggest something along the lines of

个人意见,但我会提出一些建议

public string getContestIdAsString(string ConetestId);

public int getContestIdAsInt(string ContestId);

Very obvious what each one is doing and you get around your problem. Unless there is something that I am missing.

很明显每个人都在做什么,你解决了你的问题。除非有我遗失的东西。

#5


public T GetParameter<T>(string parameterName)
{
   //Do work
   return value
}

string contestId = getParameter<string>("contestId")
int contestId = getParameter<int>("contestId")

This is an example of your best bet.

这是您最好的选择的一个例子。