Results 1 to 5 of 5

Thread: Inheritance of Template in C#

  1. #1
    Join Date
    Aug 2009
    Posts
    53

    Inheritance of Template in C#

    I want to know how to create a generic class (Template) who inherits the type passed as a parameter template:

    typically:
    Code:
    class myClass<T> : T
    {
    // new properties 
    }
    To add new property to an existing object

    Note: Version Code:
    Code:
    class myClass <T> 
    {
    T _objects; 
    // new properties 
    }
    does not interest me too because _objects properties are not accessible directly ...

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Inheritance of Template in C#

    It did not seem possible that a class should inherit a known type. There is a moment where you can not do with generics, and you must declare your classes at one time or another! Remember also that you can pass through interfaces (or even every beast of inheritance class), which perhaps will solve your problem.

    Code:
        public interface IFoo
           {
               string property1 { get; set; }
               int property2 { get; set; }
           }
           public class Foo : IFoo
           {
               string property1 { get; set; }
               int property2 { get; set; }
           }
           public class Foo2 : IFoo
           {
               string property1 { get; set; }
               int property2 { get; set; }
           }

  3. #3
    Join Date
    Aug 2009
    Posts
    53

    Re: Inheritance of Template in C#

    Indeed, upon reflection, it does not seem possible, particularly in the case where T is an interface (implementation myClass incomplete ...)

    Otherwise, if
    Code:
    class myClass<T>
       {
          T _object;
          //new properties
       }
    Is there a way to make available all properties _objects at myClass, without having to manually reset?
    Code:
    public val { get { return _object.val;}
                          set { _object.val = value;}}
    If a property contains _objects val recess

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Inheritance of Template in C#

    No, this is not possible, but you can do much better, if I understand your problem. You know the code you put above is possible if you declare that:
    Code:
        class myClass<T> where T : IFoo
         
        and
         
        public interface IFoo
        {
        int val {get;set;}
        }
    It must explicitly provide an interface (or class inheritance) in order to _object.val. If val is not known.

    Therefore your issue may be resolved otherwise. You inherit MyClass your interface (or class! Which is more convenient) and you add your new properties in myClass.

    Genericity seems unnecessary for your problem.

  5. #5
    Join Date
    Aug 2009
    Posts
    53

    Re: Inheritance of Template in C#

    Maybe I'd have to expose my problem more clearly from the beginning:

    I have a series of classes that I want to display properties via a gridview

    Code:
    class A
    {
      int _intA;
      public  int IntA {get {return _intA;}}
      bool _boolA;
      public bool BoolA {get {return _boolA;}} 
    }
    class B
    {
      float _floatB;
      public  float FloatB {get {return _floatB;}}
      string _stringA;
      public  StringA{get {return _stringA;}} 
    }
    ...
    For these classes, I would like to add properties
    Code:
    public  string Property1{get ...}
    public  string Property2{get ...}
    for the final classes
    Code:
    class A_evolved
    {
      int _intA;
      public  int IntA {get {return _intA;}}
      bool _boolA;
      public bool BoolA {get {return _boolA;}} 
      public  string Property1{get ...}
      public  string Property2{get ...}
    }
    class B_evolved
    {
      float _floatB;
      public  float FloatB {get {return _floatB;}}
      string _stringA;
      public  StringA{get {return _stringA;}} 
      public  string Property1{get ...}
      public  string Property2{get ...}
    }
    ...
    As multiple inheritance does not exist. Either I define an interface and advanced I inherit my advanced classes that interface
    Code:
    public interface IEvolved
    {
      string Property1{get ...}
      string Property2{get ...}
    }
    class A_evolved: A, IEvolved
    {
      string _property1;
      public  string Property1{get {return _property1;}}
      string _property2;
      public  string Property2{get {return _property2;}}
    }
    ...
    But I must do it for each class (unthinkable). Let (fallback) it encapsulates my base class in a class template
    Code:
    class Evoluee<T>
    {
      public Evolved(T source) : {object = sources;}
      T _object;
      string _property1;
      public  string Property1{get {return _property1;}}
      string _property2;
      public  string Property2{get {return _property2;}}
    }
    using A_evoluee = Evolved<A>;
    using B_evoluee = Evolved<B>;
    But in this case the properties of my base class (my _objects) are more accessible and will not appear automatically ...

    Any ideas?

Similar Threads

  1. Replies: 2
    Last Post: 28-01-2012, 03:50 PM
  2. Replies: 3
    Last Post: 08-01-2011, 06:32 AM
  3. What is an Inheritance in C#?
    By - Empty Shell - in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 07:03 AM
  4. What are the different types of Inheritance in C++?
    By Techno Guru in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 01:37 PM
  5. How to create a template of template
    By Harmony60 in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 05:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,293,438.62561 seconds with 17 queries