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 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