开发者

Cast an Object to a generic class when the generic type isn't known

开发者 https://www.devze.com 2023-01-10 21:40 出处:网络
In a Silverlight project, I\'ve got the following class defined. public class ThemeResource<T> { public string Name { get; set; }

In a Silverlight project, I've got the following class defined.

public class ThemeResource<T>
{
    public string Name { get; set; }
    public string Description { get; set; }
    public Type Type { get { return typeof(T); } }
    public string TypeName { get { return Type.FullName; } }
    public T Resource { get { return (T)Application.Current.Resources[this.Name]; } }
}

With several lists using the class in my code-behind.

public List<ThemeResource<SolidColorBrush>> BrushResources { get; set; }
public List<ThemeResource<Color>> ColorResources { get; set; }
public List<ThemeResource<FontFamily>> FontNameResources { get; set; }
public List<ThemeResource<Thickness>> ThicknessResources { get; set; }
pu开发者_运维百科blic List<ThemeResource<Double>> FontSizeResources { get; set; }
public List<ThemeResource<Style>> TextStyleResources { get; set; }

And my view has several list boxes bound to the lists, like this.

<UserControl x:Name="ThemeResourcesPage" ...>
    <ListBox Style="{StaticResource BrushResourceListBoxStyle}"
                ItemsSource="{Binding ElementName=ThemeResourcesPage, Path=BrushResources}"
                SelectionChanged="ListBox_SelectionChanged" />
</UserControl>

I want to have a single SelectionChanged event handler in my code-behind that all of my lists use to display details about the ThemeResource that was selected. My problem is that the SelectionChangedEventArgs' AddedItems is a list of objects, and the SelectedItem property on the ListBox is an object.

This won't work:

private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count != 1)
        return;

    var tr = (ThemeResource<T>)e.AddedItems[0];
    var msg = string.Format("Name: {0}\nType: {1}\nDescription: {2}",
                            tr.Name, tr.TypeName, tr.Description);
    MessageBox.Show(msg);
}

How do I cast the object to ThemeResource without knowing what T will be?


You can't. The usual solution would be something like this:

public interface IThemeResource
{
    public string Name { get; }
    public string Description { get; }
    public string TypeName { get; }
}

public class ThemeResource<T> : IThemeResource
{
    public string Name { get; set; }
    public string Description { get; set; }
    public Type Type { get { return typeof(T); } }
    public string TypeName { get { return Type.FullName; } }
    public T Resource { get { return (T)Application.Current.Resources[this.Name]; } }
}

Then you can pass around IThemeResources and get their properties regardless of their concrete generic type.

(Perhaps your problem admits some other, simpler solution than using generic types for this at all, but I'll leave that for someone else to come up with.)

0

精彩评论

暂无评论...
验证码 换一张
取 消