简体   繁体   中英

Can I Cast a Generic List by Type?

        NavigatorItem NavItem = (NavigatorItem)cboItems.SelectedItem;

        lblTitle.Text = NavItem.Title;

        RadWrapPanel Panel = new RadWrapPanel();

        Type t = NavItem.ItemsType; //<------ The Type inside my List is here.

        List<???> items = (List<???>)NavItem.Items; // <----Here Is the problem

        foreach (object item in items)
        {
              Panel.Children.Add((UIElement)Activator.CreateInstance(NavItem.Display,item));
        }

        ItemsContainer.Content = Panel;

In code above i need to get the type of items on t variable to put into of my generic List.

Help Please!!!

The whole idea of generics is that the type will be known at compile time. That's what makes it so fast. In your case the type is only known at runtime

Perhaps consider using object as a type in the list? Or (if you have any) the base class of the possible alternatives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM