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