简体   繁体   English

C#:如何将通用方法与“ out”变量一起使用

[英]C#: How to use generic method with “out” variable

I want to create a simple generic function 我想创建一个简单的泛型函数

void Assign<T>(out T result) 
{
  Type type = typeof(T);
  if (type.Name == "String")
  {
     // result = "hello";
  }
  else if (type.Name == "Int32")
  {
     // result = 100;
  } 
  else result = default(T);
}

Usage: 用法:

int value;
string text;

Assign(value); // <<< should set value to 100
Assign(text); // <<< should set text to "hello"

My question is how do you program the code to set these values ie. 我的问题是如何编程代码来设置这些值。 the missing codes in comment section. 注释部分中缺少的代码。

Thanks for any help. 谢谢你的帮助。

It looks like in this case maybe you're doing it to try to avoid boxing? 看起来在这种情况下,您可能正在尝试避免装箱? Difficult to say without more information, but for this specific example, it'd be much easier and probably less bug-prone to just use method overloading: 无需更多信息就很难说,但是对于此特定示例,仅使用方法重载会容易得多,并且可能不易出错:

void Assign(out string value)
{
   //...
}

void Assign(out int value)
{
   //...
}

For the purposes of learning specifically what is wrong here, you do need to cast a value to an object before casting it to the generic type: 对于具体学习什么错在这里的目的,你需要它铸造的一般类型之前将值转换为一个对象:

(T)(object)"hello world!";

Which IMO is pretty nasty and should be a last resort - certainly doesn't make your code any cleaner. 哪个IMO非常讨厌,应该是不得已的方法-当然不会使您的代码更清晰。

Any time you do type-checking of generic parameters, it's a good indication generics are not the right solution to your problem. 每当您对通用参数进行类型检查时,这都很好地表明了通用不是解决问题的正确方法。 Doing generic parameter type checks makes your code more complex, not simpler. 进行通用参数类型检查会使您的代码更复杂,而不是更简单。 It makes one method responsible for different behaviors based on type, instead of a series of single methods that are easy to change without accidentally affecting the others. 它使一个方法根据类型负责不同的行为,而不是一系列易于更改而不会意外影响其他方法的单一方法。 See Single Responsibility Principle . 请参阅单一责任原则

First of all that's a very bad pattern. 首先,这是一个非常糟糕的模式。 You shouldn't use this kind of pattern. 您不应该使用这种模式。 Maybe if you describe what you really want to achieve there will be better answers. 也许如果您描述您真正想要实现的目标,将会得到更好的答案。

Code below works, but as I said writing code this way is a bad idea. 下面的代码可行,但是正如我所说的那样编写代码是一个坏主意。

    void Assign<T>(out T result) 
    { 
        Type type = typeof(T); 
        if (type.Name == "String") 
        { result = (T) ((object)"hello"); } 
        else if (type.Name == "Int32") 
        { result = (T) ((object)100); } 
        else result = default(T); 
    }

And usage: 和用法:

        int value;
        string text;

        Assign(out value);
        Assign(out text);
public T GetObject<T>(string val)
{
    T _object = default(T);
    _object = (T)Convert.ChangeType(val, typeof(T));
    return _object;
}

Here is one way: 这是一种方法:

static void Assign<T>(out T result) { 
    Type type = typeof(T);
    if (type.Name == "String") {
        result = (T)Convert.ChangeType("hello", typeof(T));
    }
    else if (type.Name == "Int32") {
        result = (T)Convert.ChangeType(100, typeof(T));
    }
    else {
        result = default(T);
    }
}

But this code smells really bad and goes against the point of generics (instead use overloaded methods). 但是这段代码闻起来确实很糟糕,并且与泛型的观点背道而驰(而不是使用重载方法)。 I hope this doesn't end up in production code somewhere and is merely for edification. 我希望这不会最终出现在生产代码中,而只是为了启发。

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

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