[3.5] Class<T> adapter une méthode en fonction de T
Salut,
Je travaille sur un composant qui permet de définir des périodes.
Les paramètres d'entrée sont :
Propriété Debut (soit un int, soit une DateTime : T)
Propriété Duree toujours un int
en sortie on a une propriété Fin (soit un int, soit une DateTime : T)
dans le cas où mon type générique est int
Fin = Debut + Duree
dans le cas du DateTime
Fin = Debut.AddDays(Duree)
J'essaye de câbler ma propriété publique Fin et c'est là que je suis à cours de matière grise
si j'utilise
Code:
1 2 3 4 5 6 7 8
| public T Fin {
get {
if (typeof(T) == typeof(DateTime)) {
//Fin = ((DateTime)Debut).AddDays(Duree); MARCHE PAS
}
;}
} |
C'est possible d'accéder aux méthodes de l'instance en fonction du type ?
Merci
Laurent
Bon j'ai fini par trouver une solution
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
public class Period<T>
{
public T Debut { get; set; }
public int Duree { get; set; }
public T Fin
{
get
{
if (typeof(T) == typeof(DateTime))
{
T fin = (T)CalculFin(Debut as DateTime?, Duree);
return fin;
}
else if (typeof(T) == typeof(int))
{
T fin = (T)CalculFin(Debut as int?, Duree);
return fin;
}
else
{
throw new Exception("Type non supporté");
}
}
}
private object CalculFin(int? debut, int duree)
{
if (debut.HasValue)
{
return debut.Value + this.Duree;
}
else
{
return null;
}
}
private object CalculFin(DateTime? debut, int duree)
{
if (debut.HasValue)
{
return debut.Value.AddDays(this.Duree);
}
else
{
return null;
}
}
public Period(T debut,int duree)
{
this.Debut = debut;
this.Duree = duree;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdOk_Click(object sender, EventArgs e)
{
CultureInfo Culture = new CultureInfo("fr-fr");
int intDeb = -1;
int duree = 0;
int.TryParse(txtDuree.Text, out duree);
DateTime dteDeb = new DateTime();
if (int.TryParse(txtDebut.Text, out intDeb))
{
Period<int> Period = new Period<int>(intDeb, duree);
txtFin.Text = Period.Fin.ToString();
}
else if (DateTime.TryParse(txtDebut.Text,out dteDeb)) {
Period<DateTime> Period = new Period<DateTime>(dteDeb, duree);
txtFin.Text = Period.Fin.ToString("d", Culture);
};
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Maquettes._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Debut<asp:TextBox ID="txtDebut" runat="server"></asp:TextBox><br />
Duree<asp:TextBox ID="txtDuree" runat="server"></asp:TextBox><br />
fin<asp:TextBox ID="txtFin" runat="server" ReadOnly="true"></asp:TextBox><br />
<asp:Button ID="cmdOk" runat="server" Text="Button" onclick="cmdOk_Click" />
</form>
</body>
</html> |
Si on entre un int, ca calcule la somme avec la durée,
si on entre une date ca lui ajoute la durée en jours.
++
Laurent