Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A really stupid / simple re. generics and their base type

A really stupid / simple re. generics and their base type

Scheduled Pinned Locked Moved C#
questionhelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    malharone
    wrote on last edited by
    #1

    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> and List<Company>:List<T>, how do I get "List" as the inner base type of List<Employee> and List<Company>? If I do GetType() on my _listEmployees, I get System.Collections.Generic.List`1[[Employee]]. And the base type of this type (e.g. _listEmployees.GetType().BaseType), it is System.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 called GenericValueExpression<T> and concrete classes NumericValueExpression, TextValueExpression, DateValueExpression which derive from GenericValueExpression<int>, GenericValueExpression<string>, and GenericValueExpression<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

    C 1 Reply Last reply
    0
    • M malharone

      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> and List<Company>:List<T>, how do I get "List" as the inner base type of List<Employee> and List<Company>? If I do GetType() on my _listEmployees, I get System.Collections.Generic.List`1[[Employee]]. And the base type of this type (e.g. _listEmployees.GetType().BaseType), it is System.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 called GenericValueExpression<T> and concrete classes NumericValueExpression, TextValueExpression, DateValueExpression which derive from GenericValueExpression<int>, GenericValueExpression<string>, and GenericValueExpression<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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      M 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        M Offline
        M Offline
        malharone
        wrote on last edited by
        #3

        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?

        C 1 Reply Last reply
        0
        • M malharone

          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?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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 )

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups