A really stupid / simple re. generics and their base type
-
I don't know how to do this, and I searched Google/MSDN for it also but none helped -- may be I typed in the wrong search keywords ! Anyways, what's the base type of a generic class? E.g. Assume I've a
List<Employee>:List<T>
andList<Company>:List<T>
, how do I get "List" as the inner base type ofList<Employee>
andList<Company>
? If I doGetType()
on my_listEmployees
, I getSystem.Collections.Generic.List`1[[Employee]]
. And the base type of this type (e.g._listEmployees.GetType().BaseType
), it isSystem.Object
. I'd like to use "is" keyword on my generic objects to compare them. One more question on the "generic" declarations. In my app, I've an abstract class calledGenericValueExpression<T>
and concrete classesNumericValueExpression
,TextValueExpression
,DateValueExpression
which derive fromGenericValueExpression<int>
,GenericValueExpression<string>
, andGenericValueExpression<DateTime>
respectively. How can I declare a collection of GenericValueExpressions? E.g.List< GenericValueExpression<???> > _myValueExpressions
? What would go instead of ????. Thanks for your help. - Malhar -
I don't know how to do this, and I searched Google/MSDN for it also but none helped -- may be I typed in the wrong search keywords ! Anyways, what's the base type of a generic class? E.g. Assume I've a
List<Employee>:List<T>
andList<Company>:List<T>
, how do I get "List" as the inner base type ofList<Employee>
andList<Company>
? If I doGetType()
on my_listEmployees
, I getSystem.Collections.Generic.List`1[[Employee]]
. And the base type of this type (e.g._listEmployees.GetType().BaseType
), it isSystem.Object
. I'd like to use "is" keyword on my generic objects to compare them. One more question on the "generic" declarations. In my app, I've an abstract class calledGenericValueExpression<T>
and concrete classesNumericValueExpression
,TextValueExpression
,DateValueExpression
which derive fromGenericValueExpression<int>
,GenericValueExpression<string>
, andGenericValueExpression<DateTime>
respectively. How can I declare a collection of GenericValueExpressions? E.g.List< GenericValueExpression<???> > _myValueExpressions
? What would go instead of ????. Thanks for your help. - MalharAny generic/templated class system is essentially a class factory. Without a type, the class doesn't really exist, and each type means a new class is created. The beauty of .NET is that it's created in memory, for C++, it would be created in your exe. I'd suggest the easiest way to do your collection, is to define an interface for the common functionality, and then create a collection of that type. But, I'm not sure if that would work, if the methods take or return T, as they invariably would.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Any generic/templated class system is essentially a class factory. Without a type, the class doesn't really exist, and each type means a new class is created. The beauty of .NET is that it's created in memory, for C++, it would be created in your exe. I'd suggest the easiest way to do your collection, is to define an interface for the common functionality, and then create a collection of that type. But, I'm not sure if that would work, if the methods take or return T, as they invariably would.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Sorry for the delayed reply. But IMO this really seems like a missing functionality in language. Simple things such as not being able to have a collection of Nullable types makes me wonder if this was intentional or something they overlooked. e.g.
List<Nullable> myNullableValuesCollection = new List<Nullable>();
Here's my real requirement: I have the following classes/interfaces:- IDomainObject - DomainObject : IDomainObject - Employee : DomainObject - Company : DomainObject - Project : DomainObject
I've the following UI elements:- IDataboundView <T> where T:IDomainObject - BaseView : UserControl - EmployeeManagementView : BaseView, IDataboundView<Employee> - CompanyManagementView : BaseView, IDataboundView<Company> - ProjectManagementView : BaseView, IDataboundView<Project>
In the BaseView's load method, I'd like to be able to do something like ...void OnLoad(..)
{
if (this is IDataboundView)
{
Get the generic type parameter for the IDataboudView (e.g. Employee, Company, Project...)
Get the list of IDomainObjects (e.g. EmployeeColl/CompanyColl/ProjectColl etc)
Call the IDataboundView's method for "this" instance and set the collection
}
}But I cannot achieve the above. Do you have any suggestions?
-
Sorry for the delayed reply. But IMO this really seems like a missing functionality in language. Simple things such as not being able to have a collection of Nullable types makes me wonder if this was intentional or something they overlooked. e.g.
List<Nullable> myNullableValuesCollection = new List<Nullable>();
Here's my real requirement: I have the following classes/interfaces:- IDomainObject - DomainObject : IDomainObject - Employee : DomainObject - Company : DomainObject - Project : DomainObject
I've the following UI elements:- IDataboundView <T> where T:IDomainObject - BaseView : UserControl - EmployeeManagementView : BaseView, IDataboundView<Employee> - CompanyManagementView : BaseView, IDataboundView<Company> - ProjectManagementView : BaseView, IDataboundView<Project>
In the BaseView's load method, I'd like to be able to do something like ...void OnLoad(..)
{
if (this is IDataboundView)
{
Get the generic type parameter for the IDataboudView (e.g. Employee, Company, Project...)
Get the list of IDomainObjects (e.g. EmployeeColl/CompanyColl/ProjectColl etc)
Call the IDataboundView's method for "this" instance and set the collection
}
}But I cannot achieve the above. Do you have any suggestions?
What you can do is have an instance of T as a member variable, and use 'is' on that. C++ templates were certainly more powerful, but, realistically, most people used them in the ways that generics are now used.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )