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. need help with code with reference and generic function

need help with code with reference and generic function

Scheduled Pinned Locked Moved C#
helptutorialquestion
6 Posts 6 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.
  • N Offline
    N Offline
    neodeaths
    wrote on last edited by
    #1

    anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.

    public static List> DBlist;

    DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
    DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));

    foreach (Tuple<Object, string, Type> i in DBlist)
    {
    Type temp = i.Item3;
    i.Item1 = load(i.Item2);
    }

    public static T load(string tablename)

    Richard Andrew x64R L Richard DeemingR M 4 Replies Last reply
    0
    • N neodeaths

      anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.

      public static List> DBlist;

      DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
      DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));

      foreach (Tuple<Object, string, Type> i in DBlist)
      {
      Type temp = i.Item3;
      i.Item1 = load(i.Item2);
      }

      public static T load(string tablename)

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      What error are you getting? "It didn't work" is hardly enough information for anyone to help you.

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • N neodeaths

        anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.

        public static List> DBlist;

        DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
        DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));

        foreach (Tuple<Object, string, Type> i in DBlist)
        {
        Type temp = i.Item3;
        i.Item1 = load(i.Item2);
        }

        public static T load(string tablename)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        i.Item1 = load(i.Item2);
        // shouldn't that be something like
        i.Item1 = load(i.Item2);

        F 1 Reply Last reply
        0
        • L Lost User

          i.Item1 = load(i.Item2);
          // shouldn't that be something like
          i.Item1 = load(i.Item2);

          F Offline
          F Offline
          Freak30
          wrote on last edited by
          #4

          It should be rather

          i.Item1 = load(i.Item2);

          or

          i.Item1 = load(i.Item2);

          The good thing about pessimism is, that you are always either right or pleasently surprised.

          1 Reply Last reply
          0
          • N neodeaths

            anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.

            public static List> DBlist;

            DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
            DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));

            foreach (Tuple<Object, string, Type> i in DBlist)
            {
            Type temp = i.Item3;
            i.Item1 = load(i.Item2);
            }

            public static T load(string tablename)

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            So you want to call a generic method with a dynamic type parameter? You'll need to use reflection to do that. Try something like this:

            // Get the MethodInfo for the generic method:
            MethodInfo baseMethod = typeof(YourClass).GetMethod("load",
            BindingFlags.Public | BindingFlags.Static,
            /* binder = */ null,
            /* types = */ new[] { typeof(string) },
            /* modifiers = */ null);

            foreach (Tuple<object, string, Type> i in DBlist)
            {
            // Substitute the type parameter in the generic method:
            MethodInfo realMethod = baseMethod.MakeGenericMethod(i.Item3);

            // Call the static method, passing i.Item2 as the single parameter:
            i.Item1 = realMethod.Invoke(null, new\[\] { i.Item2 });
            

            }

            Where YourClass is the name of the class which contains the load<T> method.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • N neodeaths

              anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.

              public static List> DBlist;

              DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
              DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));

              foreach (Tuple<Object, string, Type> i in DBlist)
              {
              Type temp = i.Item3;
              i.Item1 = load(i.Item2);
              }

              public static T load(string tablename)

              M Offline
              M Offline
              Matt T Heffron
              wrote on last edited by
              #6

              You have several problems, as Richard Deeming noted you can't use the load that way, you'll need to use Reflection. Also, it looks like you intend to assign the result of the load into the variable referenced by the .Item1 object of the Tuple. What your code actually does is try to assign into the .Item1 property of the Tuple. Tuples, once created, cannot be changed. The .Item1 property is get-only. You might be better off changing the DBList to hold delegates that actually do what you seem to want to happen. Something like:

              public static List<Action> DBList;

              DBList.Add(() => A = load<("A"));
              DBList.Add(() => B = load<Boat>("B"));

              foreach (var loader in DBList)
              {
              loader();
              }

              public static List<T> load<T>(string tablename)

              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