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. Reflection- trying to use reflection in my project, with the code given on codeproject.com itself, however it is not working.. Plz help!!

Reflection- trying to use reflection in my project, with the code given on codeproject.com itself, however it is not working.. Plz help!!

Scheduled Pinned Locked Moved C#
helpcom
7 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.
  • P Offline
    P Offline
    Puneet Bhatnagar
    wrote on last edited by
    #1

    Here is the code snippet, with Reflection code. I have taken the code for Reflection from below link "http://www.codeproject.com/KB/cs/C\_\_Reflection\_Tutorial.aspx" public bool SaveLoginInfo(string UserName, string Password) { if (objForDBTransactions == null) { objForDBTransactions = new RegMgr(); } try { bool bSaveDone = false; object[] ArrayParam = new object[] { UserName, Password, objForDBTransactions }; //=================================================================================================== //Way 1: It is throwing an error - Object reference not set to the instance of the object //Type Objtype = Type.GetType("Login"); //MethodInfo myMethodInfo = Objtype.GetMethod("Save"); //myMethodInfo.Invoke(Objtype, ArrayParam); //=================================================================================== //Way 2: It is throwing an error - AddressBook.Login.Save does not exist Type ObjType = typeof(Login); Object obj = Activator.CreateInstance(ObjType); bSaveDone = (bool)ObjType.InvokeMember("Save ", BindingFlags.InvokeMethod, null, obj, ArrayParam); // Tried with Save, Save, Save however none is working. //=================================================================================================== return bSaveDone; } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); System.Windows.Forms.MessageBox.Show(e.Source); return false; } } Here is Save function of Login class public bool Save (T UserName, T Password, RegMgr objRegmgr) { this.UserName = UserName.ToString(); this.Password = Password.ToString(); objRegmgr.SaveLoginInfo(this); return true; } Thanks in Advance

    N L M 3 Replies Last reply
    0
    • P Puneet Bhatnagar

      Here is the code snippet, with Reflection code. I have taken the code for Reflection from below link "http://www.codeproject.com/KB/cs/C\_\_Reflection\_Tutorial.aspx" public bool SaveLoginInfo(string UserName, string Password) { if (objForDBTransactions == null) { objForDBTransactions = new RegMgr(); } try { bool bSaveDone = false; object[] ArrayParam = new object[] { UserName, Password, objForDBTransactions }; //=================================================================================================== //Way 1: It is throwing an error - Object reference not set to the instance of the object //Type Objtype = Type.GetType("Login"); //MethodInfo myMethodInfo = Objtype.GetMethod("Save"); //myMethodInfo.Invoke(Objtype, ArrayParam); //=================================================================================== //Way 2: It is throwing an error - AddressBook.Login.Save does not exist Type ObjType = typeof(Login); Object obj = Activator.CreateInstance(ObjType); bSaveDone = (bool)ObjType.InvokeMember("Save ", BindingFlags.InvokeMethod, null, obj, ArrayParam); // Tried with Save, Save, Save however none is working. //=================================================================================================== return bSaveDone; } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); System.Windows.Forms.MessageBox.Show(e.Source); return false; } } Here is Save function of Login class public bool Save (T UserName, T Password, RegMgr objRegmgr) { this.UserName = UserName.ToString(); this.Password = Password.ToString(); objRegmgr.SaveLoginInfo(this); return true; } Thanks in Advance

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      puneet.bhatnagar123@gmail.com wrote:

      http://www.codeproject.com/KB/cs/C\_\_Reflection\_Tutorial.aspx

      That article has a discussion board. Consider posting your question there with a neat subject.

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • P Puneet Bhatnagar

        Here is the code snippet, with Reflection code. I have taken the code for Reflection from below link "http://www.codeproject.com/KB/cs/C\_\_Reflection\_Tutorial.aspx" public bool SaveLoginInfo(string UserName, string Password) { if (objForDBTransactions == null) { objForDBTransactions = new RegMgr(); } try { bool bSaveDone = false; object[] ArrayParam = new object[] { UserName, Password, objForDBTransactions }; //=================================================================================================== //Way 1: It is throwing an error - Object reference not set to the instance of the object //Type Objtype = Type.GetType("Login"); //MethodInfo myMethodInfo = Objtype.GetMethod("Save"); //myMethodInfo.Invoke(Objtype, ArrayParam); //=================================================================================== //Way 2: It is throwing an error - AddressBook.Login.Save does not exist Type ObjType = typeof(Login); Object obj = Activator.CreateInstance(ObjType); bSaveDone = (bool)ObjType.InvokeMember("Save ", BindingFlags.InvokeMethod, null, obj, ArrayParam); // Tried with Save, Save, Save however none is working. //=================================================================================================== return bSaveDone; } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); System.Windows.Forms.MessageBox.Show(e.Source); return false; } } Here is Save function of Login class public bool Save (T UserName, T Password, RegMgr objRegmgr) { this.UserName = UserName.ToString(); this.Password = Password.ToString(); objRegmgr.SaveLoginInfo(this); return true; } Thanks in Advance

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        puneet.bhatnagar123@gmail.com wrote:

        Type Objtype = Type.GetType("Login");

        Please go read the documentation on MSDN, and what they say you need to pass into that function. You havent, and hence the error.

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)
        ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

        1 Reply Last reply
        0
        • P Puneet Bhatnagar

          Here is the code snippet, with Reflection code. I have taken the code for Reflection from below link "http://www.codeproject.com/KB/cs/C\_\_Reflection\_Tutorial.aspx" public bool SaveLoginInfo(string UserName, string Password) { if (objForDBTransactions == null) { objForDBTransactions = new RegMgr(); } try { bool bSaveDone = false; object[] ArrayParam = new object[] { UserName, Password, objForDBTransactions }; //=================================================================================================== //Way 1: It is throwing an error - Object reference not set to the instance of the object //Type Objtype = Type.GetType("Login"); //MethodInfo myMethodInfo = Objtype.GetMethod("Save"); //myMethodInfo.Invoke(Objtype, ArrayParam); //=================================================================================== //Way 2: It is throwing an error - AddressBook.Login.Save does not exist Type ObjType = typeof(Login); Object obj = Activator.CreateInstance(ObjType); bSaveDone = (bool)ObjType.InvokeMember("Save ", BindingFlags.InvokeMethod, null, obj, ArrayParam); // Tried with Save, Save, Save however none is working. //=================================================================================================== return bSaveDone; } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); System.Windows.Forms.MessageBox.Show(e.Source); return false; } } Here is Save function of Login class public bool Save (T UserName, T Password, RegMgr objRegmgr) { this.UserName = UserName.ToString(); this.Password = Password.ToString(); objRegmgr.SaveLoginInfo(this); return true; } Thanks in Advance

          M Offline
          M Offline
          Mark Churchill
          wrote on last edited by
          #4

          Way 3: bSaveDone = new Login().Save(UserName, Password, objForDBTransactions); Have you actually sat down and thought about why you are using generics and reflection to achieve some very simple tasks?

          Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
          Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

          P 1 Reply Last reply
          0
          • M Mark Churchill

            Way 3: bSaveDone = new Login().Save(UserName, Password, objForDBTransactions); Have you actually sat down and thought about why you are using generics and reflection to achieve some very simple tasks?

            Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
            Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

            P Offline
            P Offline
            Puneet Bhatnagar
            wrote on last edited by
            #5

            Hi, Thanks for the way3. I have already tried with that and that was working fine for me. The reason why I am using Reflection and Generics is because we gonna use these two concepts in our next project which still is in design phase. So just to learn I am using them. Thanks for your efforts. Thanks & Regards, Puneet

            M 1 Reply Last reply
            0
            • P Puneet Bhatnagar

              Hi, Thanks for the way3. I have already tried with that and that was working fine for me. The reason why I am using Reflection and Generics is because we gonna use these two concepts in our next project which still is in design phase. So just to learn I am using them. Thanks for your efforts. Thanks & Regards, Puneet

              M Offline
              M Offline
              Mark Churchill
              wrote on last edited by
              #6

              Ok, in that case: Generics in type specifications are usually done like so: Type`1[Generalisation] so like System.Collections.Generic.List`1[System.String]. I'd go out on a limb and suggest that Save`1[System.String] will work for you. Otherwise, you'll have to read the docs more.

              Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
              Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

              P 1 Reply Last reply
              0
              • M Mark Churchill

                Ok, in that case: Generics in type specifications are usually done like so: Type`1[Generalisation] so like System.Collections.Generic.List`1[System.String]. I'd go out on a limb and suggest that Save`1[System.String] will work for you. Otherwise, you'll have to read the docs more.

                Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                P Offline
                P Offline
                Puneet Bhatnagar
                wrote on last edited by
                #7

                Hi, Thanks a lot sir. I will try your solution and will definately read some more stuff on Reflection and Generics. Once again thanks for your efforts and time. Regards, Puneet

                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