Hola Leandro
Tal vez me equivoque, pero no me parace una buena forma de usar generics.
Te doy mi opinion hacer:
if (format is Mapping.PivotTableInfo.ButtonFormatInfo)
ApplyFormat<ButtonFormatInfo>(sheet, pivotTable,
(ButtonFormatInfo)format);
no es una buena práctica
Se me ocurren otras opciones:
en vez de :
private void ApplyFormat<T>(Office.Worksheet sheet, Office.PivotTable
pivotTable, T format) where T : FormatInfo
{
}
hacer:
private void ApplyFormat(Office.Worksheet sheet, Office.PivotTable
pivotTable, *Field*FormatInfo format)
{
}
private void ApplyFormat(Office.Worksheet sheet, Office.PivotTable
pivotTable, *Button*FormatInfo format)
{
}
private void ApplyFormat(Office.Worksheet sheet, Office.PivotTable
pivotTable, FormatInfo format)
{
}
Tener una firma por cada posible FormatInfo y uno que reciba el tipo base
para los que no requieren particularidades.
Me parece lo mas sano.
Yo al menos no usaría generics para esto o al menos de esa manera.
En la práctica preguntar por un type es comprar problemas....
Saludos
Daniel Calvin
El 7 de mayo de 2009 10:55, Leandro Tuttini
<[email protected]>escribió:
> Hola,
>
> Les planteo la siguiente situacion.
>
> Tengo las siguientes clases:
>
> public abstract class FormatInfo
> {
> }
>
> public class FieldFormatInfo : FormatInfo
> {
> }
>
> public class ButtonFormatInfo : FormatInfo
> {
> }
>
> public class OriginFormatInfo : FormatInfo
> {
> }
>
> public class BlankFormatInfo : FormatInfo
> {
> }
>
>
> Y la siguiente implementacion:
>
>
> foreach (FormatInfo format in pivotTableInfo.Format)
> {
> //ApplyFormat<typeof(format)>(sheet, pivotTable, format);
>
> if (format is Mapping.PivotTableInfo.ButtonFormatInfo)
> ApplyFormat<ButtonFormatInfo>(sheet, pivotTable,
> (ButtonFormatInfo)format);
> else if (format is Mapping.PivotTableInfo.FieldFormatInfo)
> ApplyFormat<FieldFormatInfo>(sheet, pivotTable,
> (FieldFormatInfo)format);
> else if (format is Mapping.PivotTableInfo.OriginFormatInfo)
> ApplyFormat<OriginFormatInfo>(sheet, pivotTable,
> (OriginFormatInfo)format);
> else if (format is Mapping.PivotTableInfo.BlankFormatInfo)
> ApplyFormat<BlankFormatInfo>(sheet, pivotTable,
> (BlankFormatInfo)format);
>
> }
>
>
> pivotTableInfo.Format, es una propiedad del tipo:
>
> public List<FormatInfo> Format { get; set; }
>
>
> Y ApplyFormat() es un metodo:
>
> private void ApplyFormat<T>(Office.Worksheet sheet, Office.PivotTable
> pivotTable, T format) where T : FormatInfo
> {
> }
>
>
>
> Como veran funciona sin problema, pero la verdad no queda nada agradable a
> la vista, en realidad yo reduje un poco el ejemplo, hay unos 6 format mas
> que hacen el if
> bastante feo.
>
> por eso la cuestion es porque no puedo hacer algo asi:
>
> foreach (FormatInfo format in pivotTableInfo.Format)
> {
> ApplyFormat<typeof(format)>(sheet, pivotTable, format);
> }
>
> O sea la aplicacion del template seria dinamica.
>
> Se puede lograr algo parecido a esto ?, o la unica forma es con los if
> descubriendo el tipo.
>
> Saludos
> ------------------------------
>
> Yahoo! Cocina
> Recetas prácticas y comida saludable
> Visitá http://ar.mujer.yahoo.com/cocina/
>
--
Daniel A. Calvin
Cooperator Team Member
http://www.cooperator.com.ar
Microsoft Certified Professional