Unable to pass Type to csla SortedAndPagedBindingList Unable to pass Type to csla SortedAndPagedBindingList
Old forum URL: forums.lhotka.net/forums/t/5917.aspx
franc23 posted on Monday, December 01, 2008
Hi,
I would like to generalize the following line:
SortCSLA.SortedAndPagedBindingList<"BusinessBase"> slSortedGrid = new SortCSLA.SortedAndPagedBindingList<"BusinessBase">("BusinessListBase - data", args, (System.ComponentModel.ListSortDirection)ViewState["SORTDIRECTION"]);
If the values for "BusinessBase" and "BusinessListBase - data" are hard coded, sorting and paging work as expected.
However, when trying for a generic solution using "BusinessBase".GetType() in the above statement, for ex:
Type aType = MemberCollection.GetType();
SortCSLA.SortedAndPagedBindingList<"aType"> slSortedGrid = new SortCSLA.SortedAndPagedBindingList<"aType">("aTypeBase - data", args, (System.ComponentModel.ListSortDirection)ViewState["SORTDIRECTION"]);
it throws an error stating:
CS0246: The type or namespace name 'aType' could not be found (are you missing a
using directive or an assembly reference?)
Any reason as to what could be wrong here. Or could I pass it in some other manner.
I tried searching on google and this site, but I didn't find anything and hence my first post here. Apologize if I missed something and I'd appreciate it if you could point me to any other links that may address this issue.
Thanks for looking. Any help will be great.
RockfordLhotka replied on Tuesday, December 02, 2008
Generics don't normally allow you to pass a Type object as the type parameter to a generic type. In other words this is fine:
var x = new List<string>();
but this is not:
var t = typeof(string);
var x = new List<t>();
If you really want to do this, then you'll need to use reflection to create an instance of the generic type. It is pretty ugly reflection, but it can be done.
Copyright (c) Marimer LLC