Bonjour à tous,
j'ai un petit problème dans mon application
.
j'ai un classe Pair, qui est une classe générique dont voici le code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Pair<T>
{
public T First { get; set; }
public object Second { get; set; }
public Pair() { }
public Pair(T first, object second)
{
First = first;
Second = second;
}
public Pair(T first)
: this(first, null)
{ }
} |
et j'ai une méthode qui me retourne un string en fonction du type du paramètre
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static string ValueToStringFormat(object someValue)
{
string FormattedValue = "";
if (someValue == null)
{
FormattedValue = "NULL";
}
else
{
switch (someValue.GetType().Name)
{
case "String": FormattedValue = "'" + ((string)someValue).Replace("'", "''") + "'"; break;
case "DateTime": FormattedValue = "'" + ((DateTime)someValue).ToString("yyyy/MM/dd hh:mm:ss") + "'"; break;
case "DBNull": FormattedValue = "NULL"; break;
case "Boolean": FormattedValue = (bool)someValue ? "1" : "0"; break;
case "Pair`1":
//?????????
break;
default: FormattedValue = someValue.ToString(); break;
}
}
return FormattedValue;
} |
Mon but est de remplacer les ????? par un truc du genre
ValueToStringFormat(someValue.First) + (someValue.Second != null) ? " And " + ValueToStringFormat(someValue) : String.Empty;
Le problème, c'est que pour ça, il faudrait que je puisse caster someValue en Pair<>, vu que je ne suis pas censé connaitre le type, mais ça ne marche pas
et je ne sais pas du tout si c'est possible.
Si quelqu'un a une idée, je l'en remercie
Partager