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. .NET (Core and Framework)
  4. how to specify the T Generic type for a class at runtime or from parameter [modified]

how to specify the T Generic type for a class at runtime or from parameter [modified]

Scheduled Pinned Locked Moved .NET (Core and Framework)
helptutorialquestion
16 Posts 4 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.
  • F Offline
    F Offline
    Feras Mazen Taleb
    wrote on last edited by
    #1

    I made class called LinqFilter

    public class LinqFilter
    {
    .......
    }

    and I made a user control

    public class lablab : usercontrol
    {
    linqFilter<[here i want to not put exact class but i want to still make it generic] > koko;

    }

    and i want to specifiy the [T] at runtime can I Do that OR can I Get the T from Parameter come from outside the usercontrol I tryid this code but i get an error

    public class lablab<z> : usercontrol
    {
    linqFilter<z> koko;
    }

    thanks a lot

    You have To Search About The Truth Of Your Life Why Are you Here In Life ?

    modified on Monday, February 9, 2009 12:25 AM

    D 1 Reply Last reply
    0
    • F Feras Mazen Taleb

      I made class called LinqFilter

      public class LinqFilter
      {
      .......
      }

      and I made a user control

      public class lablab : usercontrol
      {
      linqFilter<[here i want to not put exact class but i want to still make it generic] > koko;

      }

      and i want to specifiy the [T] at runtime can I Do that OR can I Get the T from Parameter come from outside the usercontrol I tryid this code but i get an error

      public class lablab<z> : usercontrol
      {
      linqFilter<z> koko;
      }

      thanks a lot

      You have To Search About The Truth Of Your Life Why Are you Here In Life ?

      modified on Monday, February 9, 2009 12:25 AM

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You can't. Generics are (IIRC) really just syntaxic sugar that tells the compiler to write code for each Type you specify in the Generic. Say you pass two different types of objects to a Generic method. The compiler automatically writes two methods, using your code as a template, to handle both cases of those objects passed to it. Types used by Generics are determined at compile-time, not run-time.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      S F 2 Replies Last reply
      0
      • D Dave Kreskowiak

        You can't. Generics are (IIRC) really just syntaxic sugar that tells the compiler to write code for each Type you specify in the Generic. Say you pass two different types of objects to a Generic method. The compiler automatically writes two methods, using your code as a template, to handle both cases of those objects passed to it. Types used by Generics are determined at compile-time, not run-time.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        S Offline
        S Offline
        supercat9
        wrote on last edited by
        #3

        Generic reference types are more than syntactic sugar; the compiler only has to produce one JIT-compiled instance of a method that uses a generic reference type, but the method can produce objects which are 'ready to go' in the desired type without the expense of a typecast or other run-time validation. I know of no way such a result could be achieved without generics. There are limits to what generics can do, but they seem to be a very useful feature with significant semantic meaning. Extension methods, by contrast, seem more like syntactic sugar.

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          You can't. Generics are (IIRC) really just syntaxic sugar that tells the compiler to write code for each Type you specify in the Generic. Say you pass two different types of objects to a Generic method. The compiler automatically writes two methods, using your code as a template, to handle both cases of those objects passed to it. Types used by Generics are determined at compile-time, not run-time.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          F Offline
          F Offline
          Feras Mazen Taleb
          wrote on last edited by
          #4

          thanks a lot for your replay and explanation but could you tell me how can I use the second choice I mean how to make the user control accept T as Template something like this :

          public class kokoControl : UserControl
          {
          .....
          }

          You have To Search About The Truth Of Your Life Why Are you Here In Life ?

          D 1 Reply Last reply
          0
          • F Feras Mazen Taleb

            thanks a lot for your replay and explanation but could you tell me how can I use the second choice I mean how to make the user control accept T as Template something like this :

            public class kokoControl : UserControl
            {
            .....
            }

            You have To Search About The Truth Of Your Life Why Are you Here In Life ?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            What are you ultimately trying to do with this??

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            F 1 Reply Last reply
            0
            • D Dave Kreskowiak

              What are you ultimately trying to do with this??

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              F Offline
              F Offline
              Feras Mazen Taleb
              wrote on last edited by
              #6

              ok I'm sorry my replay was not complete . i will tell you the full story :) I made generic class that can make DynamicLinq For any class this class is

              public class LinqFilter<T>
              {
              some of :
              public Fun<T,bool> GetFilter()
              {
              //here i will return
              Expression Filter ;
              Filter = Expression.And(.....)
              ..............................
              var predicate = Expression.Lambda. .... .
              return (Func<T,bool> )predicate.Compile();
              }
              }

              the previous class work well I want to use it inside a user control called FilterControl but I want to make the control also as a Generic Class just to pass the T inside the control

              public Class FilterControl<T> : UserControl
              {
              LinqFilter<T> F;
              ................
              }

              but I got an error when tried to do that how can I pass the T from outside the control to the [F] Variable of linqfilter .:confused: thanks

              You have To Search About The Truth Of Your Life Why Are you Here In Life ?

              C 1 Reply Last reply
              0
              • F Feras Mazen Taleb

                ok I'm sorry my replay was not complete . i will tell you the full story :) I made generic class that can make DynamicLinq For any class this class is

                public class LinqFilter<T>
                {
                some of :
                public Fun<T,bool> GetFilter()
                {
                //here i will return
                Expression Filter ;
                Filter = Expression.And(.....)
                ..............................
                var predicate = Expression.Lambda. .... .
                return (Func<T,bool> )predicate.Compile();
                }
                }

                the previous class work well I want to use it inside a user control called FilterControl but I want to make the control also as a Generic Class just to pass the T inside the control

                public Class FilterControl<T> : UserControl
                {
                LinqFilter<T> F;
                ................
                }

                but I got an error when tried to do that how can I pass the T from outside the control to the [F] Variable of linqfilter .:confused: thanks

                You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                C Offline
                C Offline
                Curtis Schlak
                wrote on last edited by
                #7

                So I fired up Snippet Compiler[^], added System.Core.dll as a reference, and wrote this little snippet of code:

                public class Base
                {
                public Func<t,> GetFilter()
                {
                return delegate(T t) { return true; };
                }
                }

                public class BaseCotnrol : UserControl
                {
                Base foo;
                }

                It compiled just fine. Could you post the compiler error?

                "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                F 1 Reply Last reply
                0
                • C Curtis Schlak

                  So I fired up Snippet Compiler[^], added System.Core.dll as a reference, and wrote this little snippet of code:

                  public class Base
                  {
                  public Func<t,> GetFilter()
                  {
                  return delegate(T t) { return true; };
                  }
                  }

                  public class BaseCotnrol : UserControl
                  {
                  Base foo;
                  }

                  It compiled just fine. Could you post the compiler error?

                  "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                  F Offline
                  F Offline
                  Feras Mazen Taleb
                  wrote on last edited by
                  #8

                  first , thanks a lot for your replay :) I thought that I will not get the answer :^) !! I will try that but could you explain more what should this code do ?

                  You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                  C 1 Reply Last reply
                  0
                  • F Feras Mazen Taleb

                    first , thanks a lot for your replay :) I thought that I will not get the answer :^) !! I will try that but could you explain more what should this code do ?

                    You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                    C Offline
                    C Offline
                    Curtis Schlak
                    wrote on last edited by
                    #9

                    My code doesn't really do anything. &l;smile /> I just wrote some code that looked like your code to make sure that it compiles. It compiled. Unfortunately, that just means that I don't understand the nature of your question. Can you post something more indicative of your plight?

                    "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                    F 1 Reply Last reply
                    0
                    • C Curtis Schlak

                      My code doesn't really do anything. &l;smile /> I just wrote some code that looked like your code to make sure that it compiles. It compiled. Unfortunately, that just means that I don't understand the nature of your question. Can you post something more indicative of your plight?

                      "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                      F Offline
                      F Offline
                      Feras Mazen Taleb
                      wrote on last edited by
                      #10

                      I will explain I made this class

                      public class LinqFilter<T>
                      {
                      public Fun<T,bool> GetFilter()
                      {
                      // in this function I made an Expression Tree after
                      // that i make lambda expression that return delegate
                      // this is not important for my problem
                      // but I said that just to not write empty function .
                      var predicate = Expression.Lambda. .... .
                      return (Func<T,bool> )predicate.Compile();
                      }
                      }

                      there is a user control called FilterControl I need to use object of previous class [LinqFilter] inside this control but I want to get The [T] Type from out side the user control the following code make compiler error when i put <T> in User Control definition

                      public Class FilterControl<T> : UserControl
                      {
                      LinqFilter<T> F;
                      ................
                      }

                      I hope to not give me joking code again thanks

                      You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                      C 1 Reply Last reply
                      0
                      • F Feras Mazen Taleb

                        I will explain I made this class

                        public class LinqFilter<T>
                        {
                        public Fun<T,bool> GetFilter()
                        {
                        // in this function I made an Expression Tree after
                        // that i make lambda expression that return delegate
                        // this is not important for my problem
                        // but I said that just to not write empty function .
                        var predicate = Expression.Lambda. .... .
                        return (Func<T,bool> )predicate.Compile();
                        }
                        }

                        there is a user control called FilterControl I need to use object of previous class [LinqFilter] inside this control but I want to get The [T] Type from out side the user control the following code make compiler error when i put <T> in User Control definition

                        public Class FilterControl<T> : UserControl
                        {
                        LinqFilter<T> F;
                        ................
                        }

                        I hope to not give me joking code again thanks

                        You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                        C Offline
                        C Offline
                        Curtis Schlak
                        wrote on last edited by
                        #11

                        When you write that you "want to get the [T] Type from out side the user control", what do you mean by that? Do you mean that you want to bind T at runtime rather than at compile time? Something like the following pseudocode?

                        t <- assign some resolved Type
                        create an instance of FilterControl dynamically and bind t to T
                        put the instance of FilterControl`1 on your form

                        "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                        F 1 Reply Last reply
                        0
                        • C Curtis Schlak

                          When you write that you "want to get the [T] Type from out side the user control", what do you mean by that? Do you mean that you want to bind T at runtime rather than at compile time? Something like the following pseudocode?

                          t <- assign some resolved Type
                          create an instance of FilterControl dynamically and bind t to T
                          put the instance of FilterControl`1 on your form

                          "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                          F Offline
                          F Offline
                          Feras Mazen Taleb
                          wrote on last edited by
                          #12

                          What I need is : When I put the FilterControl in the window , I want to specify the T Type I mean in Compile Time in this way the control will be able to make filtering on objects of any T class with support from LinqFilter class

                          You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                          C 1 Reply Last reply
                          0
                          • F Feras Mazen Taleb

                            What I need is : When I put the FilterControl in the window , I want to specify the T Type I mean in Compile Time in this way the control will be able to make filtering on objects of any T class with support from LinqFilter class

                            You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                            C Offline
                            C Offline
                            Curtis Schlak
                            wrote on last edited by
                            #13

                            Ah ha! Now I understand. The Visual Studio designers do not support controls with unbound generic parameters. You can't create your FilterControl<T> : UserControl and then use the forms designer to drag it onto a panel or form. You might be able to adapt a TypeDescriptor for the user control for your forms, though I've never seen this done. There's an article on Urban Potato that does this for controls with abstract base classes: Using Visual Studio Whidbey to Design Abstract Forms[^].

                            F 1 Reply Last reply
                            0
                            • C Curtis Schlak

                              Ah ha! Now I understand. The Visual Studio designers do not support controls with unbound generic parameters. You can't create your FilterControl<T> : UserControl and then use the forms designer to drag it onto a panel or form. You might be able to adapt a TypeDescriptor for the user control for your forms, though I've never seen this done. There's an article on Urban Potato that does this for controls with abstract base classes: Using Visual Studio Whidbey to Design Abstract Forms[^].

                              F Offline
                              F Offline
                              Feras Mazen Taleb
                              wrote on last edited by
                              #14

                              thanks a lot I will read this article ( in sha'a allah) and I hope to find what I want Thanks very match :)

                              You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                              C 1 Reply Last reply
                              0
                              • F Feras Mazen Taleb

                                thanks a lot I will read this article ( in sha'a allah) and I hope to find what I want Thanks very match :)

                                You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                                C Offline
                                C Offline
                                Curtis Schlak
                                wrote on last edited by
                                #15

                                If you find or develop something interesting, then don't forget to come back and post a solution so that we all can learn! :-D

                                "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                                F 1 Reply Last reply
                                0
                                • C Curtis Schlak

                                  If you find or develop something interesting, then don't forget to come back and post a solution so that we all can learn! :-D

                                  "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                                  F Offline
                                  F Offline
                                  Feras Mazen Taleb
                                  wrote on last edited by
                                  #16

                                  Okay my friend :)

                                  You have To Search About The Truth Of Your Life Why Are you Here In Life ?

                                  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