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. using reflection

using reflection

Scheduled Pinned Locked Moved C#
help
4 Posts 3 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
    mmdullah
    wrote on last edited by
    #1

    public Ts GetMany(BasicBO BO) where T : BasicBO { List BOList = DataAccess.Get(tc, BO); List TList = new List(); foreach (BasicBO bo in BOList) TList.Add((T)bo); // error occured in the following statement object result = typeof(Ts).GetType() .InvokeMember("ctor",BindingFlags.CreateInstance, null, null, null); typeof(Ts).GetMethod("AddRangeFromDB", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(result, new object[] { TList }); return (Ts)result; } error message is : Constructor on type 'System.RuntimeType' not found Ts is a generic list of basicBO. please explain it.

    D P 2 Replies Last reply
    0
    • M mmdullah

      public Ts GetMany(BasicBO BO) where T : BasicBO { List BOList = DataAccess.Get(tc, BO); List TList = new List(); foreach (BasicBO bo in BOList) TList.Add((T)bo); // error occured in the following statement object result = typeof(Ts).GetType() .InvokeMember("ctor",BindingFlags.CreateInstance, null, null, null); typeof(Ts).GetMethod("AddRangeFromDB", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(result, new object[] { TList }); return (Ts)result; } error message is : Constructor on type 'System.RuntimeType' not found Ts is a generic list of basicBO. please explain it.

      D Offline
      D Offline
      dybs
      wrote on last edited by
      #2

      I'm not at my dev machine so I can't test this, but try removing GetType() from the line so you just have

      object result = typeof(Ts).InvokeMember("ctor", BindingFlags.CreateInstance, null, null, null);

      My bet is that the Type instance you want is from typeof(Ts), not typeof(Ts).GetType(). Dybs

      The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

      M 1 Reply Last reply
      0
      • M mmdullah

        public Ts GetMany(BasicBO BO) where T : BasicBO { List BOList = DataAccess.Get(tc, BO); List TList = new List(); foreach (BasicBO bo in BOList) TList.Add((T)bo); // error occured in the following statement object result = typeof(Ts).GetType() .InvokeMember("ctor",BindingFlags.CreateInstance, null, null, null); typeof(Ts).GetMethod("AddRangeFromDB", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(result, new object[] { TList }); return (Ts)result; } error message is : Constructor on type 'System.RuntimeType' not found Ts is a generic list of basicBO. please explain it.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        If it's generic, you must have lost your generic parameters. You may also want to specify New in the where clause so you can instantiate the class directly rather than use Reflection. But I think one of the main problems is that the constructor's name is ".ctor". Also, when using Reflection, you may want to cache the MethodInfo rather than get it over and over again.

        1 Reply Last reply
        0
        • D dybs

          I'm not at my dev machine so I can't test this, but try removing GetType() from the line so you just have

          object result = typeof(Ts).InvokeMember("ctor", BindingFlags.CreateInstance, null, null, null);

          My bet is that the Type instance you want is from typeof(Ts), not typeof(Ts).GetType(). Dybs

          The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

          M Offline
          M Offline
          mmdullah
          wrote on last edited by
          #4

          Thank you very much. You are right. I didnt care about recoding of the line Ts.GetType(). but it is already retrieved with typeof(Ts). Now it works fine. thank you

          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