C#: Verwendung von generischen Methode mit "out" - variable

Ich möchte erstellen Sie eine einfache generische Funktion

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

Verwendung:

int value;
string text;

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

Meine Frage ist, wie kann man das Programm den code um diese Werte dh. die fehlenden codes in Kommentar-Abschnitt.

Vielen Dank für jede Hilfe.

InformationsquelleAutor Tin Vo | 2009-11-14
Schreibe einen Kommentar