C#:如何使用带有“out”的泛型方法多变的

发布于 2024-08-11 01:10:34 字数 498 浏览 4 评论 0原文

我想创建一个简单的通用函数

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);
}

用法:

int value;
string text;

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

我的问题是如何编写代码来设置这些值,即。评论部分缺少的代码。

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巡山小妖精 2024-08-18 01:10:34

在这种情况下,看起来你这样做可能是为了避免拳击?如果没有更多信息,很难说,但对于这个特定的示例,仅使用方法重载会更容易,而且可能更不容易出错:

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

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

为了具体了解这里的错误,您可以在将值转换为泛型类型之前,确实需要将值转换为对象:

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

在我看来,这非常令人讨厌,应该是最后的手段——当然不会让你的代码变得更干净。

任何时候对泛型参数进行类型检查,都很好地表明泛型并不是解决问题的正确方法。进行泛型参数类型检查会使您的代码变得更加复杂,而不是更简单。它使一个方法负责基于类型的不同行为,而不是一系列易于更改而不会意外影响其他方法的单个方法。请参阅单一职责原则

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.

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.

花辞树 2024-08-18 01:10:34

首先,这是一个非常糟糕的模式。你不应该使用这种模式。如果你描述一下你真正想要实现的目标,也许会有更好的答案。

下面的代码可以工作,但正如我所说,以这种方式编写代码是一个坏主意。

    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); 
    }

及用法:

        int value;
        string text;

        Assign(out value);
        Assign(out text);

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);
柠檬心 2024-08-18 01:10:34
public T GetObject<T>(string val)
{
    T _object = default(T);
    _object = (T)Convert.ChangeType(val, typeof(T));
    return _object;
}
public T GetObject<T>(string val)
{
    T _object = default(T);
    _object = (T)Convert.ChangeType(val, typeof(T));
    return _object;
}
寂寞美少年 2024-08-18 01:10:34

这是一种方法:

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);
    }
}

但是这段代码闻起来非常糟糕并且违背了泛型的要点(而不是使用重载方法)。我希望这不会出现在某个地方的生产代码中,而只是为了启发。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文