Skip to content

Commit 5e28c4d

Browse files
Copilotcsharpfritz
andcommitted
Add IDataListStyleContainer interface to eliminate reflection and improve type safety
Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com>
1 parent 2dd8c86 commit 5e28c4d

7 files changed

Lines changed: 41 additions & 46 deletions

File tree

src/BlazorWebFormsComponents/AlternatingItemStyle.razor.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
using Microsoft.AspNetCore.Components;
1+
using BlazorWebFormsComponents.Interfaces;
2+
using Microsoft.AspNetCore.Components;
23

34
namespace BlazorWebFormsComponents
45
{
56
public partial class AlternatingItemStyle : UiTableItemStyle
67
{
78
[CascadingParameter(Name = "ParentDataList")]
8-
protected object ParentDataList { get; set; }
9+
protected IDataListStyleContainer ParentDataList { get; set; }
910

1011
protected override void OnInitialized()
1112
{
1213
if (ParentDataList != null)
1314
{
14-
var parentType = ParentDataList.GetType();
15-
var alternatingItemStyleProperty = parentType.GetProperty("AlternatingItemStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
16-
if (alternatingItemStyleProperty != null)
17-
{
18-
theStyle = alternatingItemStyleProperty.GetValue(ParentDataList) as TableItemStyle;
19-
}
15+
theStyle = ParentDataList.AlternatingItemStyle;
2016
}
2117
base.OnInitialized();
2218
}

src/BlazorWebFormsComponents/DataList.razor.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using BlazorComponentUtilities;
22
using BlazorWebFormsComponents.DataBinding;
33
using BlazorWebFormsComponents.Enums;
4+
using BlazorWebFormsComponents.Interfaces;
45
using Microsoft.AspNetCore.Components;
56
using System.Collections.Generic;
67
using System.Linq;
78

89
namespace BlazorWebFormsComponents
910
{
10-
public partial class DataList<ItemType> : DataBoundComponent<ItemType>, IStyle
11+
public partial class DataList<ItemType> : DataBoundComponent<ItemType>, IStyle, IDataListStyleContainer
1112
{
1213
private static readonly Dictionary<DataListEnum, string?> _GridLines = new Dictionary<DataListEnum, string?> {
1314
{DataListEnum.None, null },
@@ -25,18 +26,18 @@ public partial class DataList<ItemType> : DataBoundComponent<ItemType>, IStyle
2526
[Parameter] public DataListEnum GridLines { get; set; } = DataListEnum.None;
2627
[Parameter] public RenderFragment HeaderTemplate { get; set; }
2728
[Parameter] public RenderFragment FooterTemplate { get; set; }
28-
internal TableItemStyle HeaderStyle { get; set; } = new TableItemStyle();
29-
internal TableItemStyle FooterStyle { get; set; } = new TableItemStyle();
29+
public TableItemStyle HeaderStyle { get; internal set; } = new TableItemStyle();
30+
public TableItemStyle FooterStyle { get; internal set; } = new TableItemStyle();
3031
[Parameter] public RenderFragment HeaderStyleContent { get; set; }
3132
[Parameter] public RenderFragment FooterStyleContent { get; set; }
32-
internal TableItemStyle ItemStyle { get; set; } = new TableItemStyle();
33+
public TableItemStyle ItemStyle { get; internal set; } = new TableItemStyle();
3334
[Parameter] public RenderFragment<ItemType> ItemTemplate { get; set; }
3435
[Parameter] public RenderFragment<ItemType> AlternatingItemTemplate { get; set; }
35-
internal TableItemStyle AlternatingItemStyle { get; set; } = new TableItemStyle();
36+
public TableItemStyle AlternatingItemStyle { get; internal set; } = new TableItemStyle();
3637
[Parameter] public RepeatLayout RepeatLayout { get; set; } = BlazorWebFormsComponents.Enums.RepeatLayout.Table;
3738
[Parameter] public DataListEnum RepeatDirection { get; set; } = BlazorWebFormsComponents.Enums.DataListEnum.Vertical;
3839
[Parameter] public int RepeatColumns { get; set; } = 1;
39-
internal TableItemStyle SeparatorStyle { get; set; } = new TableItemStyle();
40+
public TableItemStyle SeparatorStyle { get; internal set; } = new TableItemStyle();
4041
[Parameter] public RenderFragment SeparatorTemplate { get; set; }
4142
[Parameter] public RenderFragment ItemStyleContent { get; set; }
4243
[Parameter] public RenderFragment AlternatingItemStyleContent { get; set; }

src/BlazorWebFormsComponents/FooterStyle.razor.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
using Microsoft.AspNetCore.Components;
1+
using BlazorWebFormsComponents.Interfaces;
2+
using Microsoft.AspNetCore.Components;
23

34
namespace BlazorWebFormsComponents
45
{
56
public partial class FooterStyle : UiTableItemStyle
67
{
78
[CascadingParameter(Name = "ParentDataList")]
8-
protected object ParentDataList { get; set; }
9+
protected IDataListStyleContainer ParentDataList { get; set; }
910

1011
protected override void OnInitialized()
1112
{
1213
if (ParentDataList != null)
1314
{
14-
var parentType = ParentDataList.GetType();
15-
var footerStyleProperty = parentType.GetProperty("FooterStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
16-
if (footerStyleProperty != null)
17-
{
18-
theStyle = footerStyleProperty.GetValue(ParentDataList) as TableItemStyle;
19-
}
15+
theStyle = ParentDataList.FooterStyle;
2016
}
2117
base.OnInitialized();
2218
}

src/BlazorWebFormsComponents/HeaderStyle.razor.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Components;
1+
using BlazorWebFormsComponents.Interfaces;
2+
using Microsoft.AspNetCore.Components;
23

34
namespace BlazorWebFormsComponents
45
{
@@ -7,18 +8,13 @@ public partial class HeaderStyle : UiTableItemStyle
78
{
89

910
[CascadingParameter(Name = "ParentDataList")]
10-
protected object ParentDataList { get; set; }
11+
protected IDataListStyleContainer ParentDataList { get; set; }
1112

1213
protected override void OnInitialized()
1314
{
1415
if (ParentDataList != null)
1516
{
16-
var parentType = ParentDataList.GetType();
17-
var headerStyleProperty = parentType.GetProperty("HeaderStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
18-
if (headerStyleProperty != null)
19-
{
20-
theStyle = headerStyleProperty.GetValue(ParentDataList) as TableItemStyle;
21-
}
17+
theStyle = ParentDataList.HeaderStyle;
2218
}
2319
base.OnInitialized();
2420
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace BlazorWebFormsComponents.Interfaces
2+
{
3+
/// <summary>
4+
/// Interface for components that contain TableItemStyle properties
5+
/// </summary>
6+
public interface IDataListStyleContainer
7+
{
8+
TableItemStyle HeaderStyle { get; }
9+
TableItemStyle FooterStyle { get; }
10+
TableItemStyle ItemStyle { get; }
11+
TableItemStyle AlternatingItemStyle { get; }
12+
TableItemStyle SeparatorStyle { get; }
13+
}
14+
}

src/BlazorWebFormsComponents/ItemStyle.razor.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
using Microsoft.AspNetCore.Components;
1+
using BlazorWebFormsComponents.Interfaces;
2+
using Microsoft.AspNetCore.Components;
23

34
namespace BlazorWebFormsComponents
45
{
56
public partial class ItemStyle : UiTableItemStyle
67
{
78

89
[CascadingParameter(Name = "ParentDataList")]
9-
protected object ParentDataList { get; set; }
10+
protected IDataListStyleContainer ParentDataList { get; set; }
1011

1112
protected override void OnInitialized()
1213
{
1314
if (ParentDataList != null)
1415
{
15-
var parentType = ParentDataList.GetType();
16-
var itemStyleProperty = parentType.GetProperty("ItemStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
17-
if (itemStyleProperty != null)
18-
{
19-
theStyle = itemStyleProperty.GetValue(ParentDataList) as TableItemStyle;
20-
}
16+
theStyle = ParentDataList.ItemStyle;
2117
}
2218
base.OnInitialized();
2319
}

src/BlazorWebFormsComponents/SeparatorStyle.razor.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
using Microsoft.AspNetCore.Components;
1+
using BlazorWebFormsComponents.Interfaces;
2+
using Microsoft.AspNetCore.Components;
23

34
namespace BlazorWebFormsComponents
45
{
56
public partial class SeparatorStyle : UiTableItemStyle
67
{
78

89
[CascadingParameter(Name = "ParentDataList")]
9-
protected object ParentDataList { get; set; }
10+
protected IDataListStyleContainer ParentDataList { get; set; }
1011

1112
protected override void OnInitialized()
1213
{
1314
if (ParentDataList != null)
1415
{
15-
var parentType = ParentDataList.GetType();
16-
var separatorStyleProperty = parentType.GetProperty("SeparatorStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
17-
if (separatorStyleProperty != null)
18-
{
19-
theStyle = separatorStyleProperty.GetValue(ParentDataList) as TableItemStyle;
20-
}
16+
theStyle = ParentDataList.SeparatorStyle;
2117
}
2218
base.OnInitialized();
2319
}

0 commit comments

Comments
 (0)