Using Activator.CreateInstance()
-
Hi, I am currently trying to dynamically create an instance of a class. My class has a member called XmlReturnData which is derived from XmlDocumentFragment. Depending on the command which was sent to a client, XmlReturnData can be specialized as - for example - XmlReturnDataLogin. A class called LoginCommand (derived from XmlCoammd which is derived from XmlDocument) calls its base methods ParseXml() passing the type information of the XmlReturnData - object to create: Public ParseXml(ByVal returnType As System.Type) At the end of the method, I am trying to create the appropriate instance of the return object: Dim args() As Object = { Me, SelectSingleNode() } m_returnData = Activator.CreateInstance(returnType, args) Now, I'll get the following exception: System.Reflection.TargetInvocationEception [...] <------ System.NullReferenceException So, what can be Null here? The exception is not thrown by the constructor of XmlReturnDataLogin, because I even don't get there. Kind Regards, Michael
-
Hi, I am currently trying to dynamically create an instance of a class. My class has a member called XmlReturnData which is derived from XmlDocumentFragment. Depending on the command which was sent to a client, XmlReturnData can be specialized as - for example - XmlReturnDataLogin. A class called LoginCommand (derived from XmlCoammd which is derived from XmlDocument) calls its base methods ParseXml() passing the type information of the XmlReturnData - object to create: Public ParseXml(ByVal returnType As System.Type) At the end of the method, I am trying to create the appropriate instance of the return object: Dim args() As Object = { Me, SelectSingleNode() } m_returnData = Activator.CreateInstance(returnType, args) Now, I'll get the following exception: System.Reflection.TargetInvocationEception [...] <------ System.NullReferenceException So, what can be Null here? The exception is not thrown by the constructor of XmlReturnDataLogin, because I even don't get there. Kind Regards, Michael
Michael Groeger wrote: m_returnData = Activator.CreateInstance(returnType, args) The documentation says the exception is thrown when the constructor throws an error; however I have found that it can be other things as well. When I wrote my "extend your .net programs at runtime with interfaces" article I was finding that sometimes I just needed to do a rebuild of all pieces of my project. It seems that the type the program expected was a different version than the one that was actually loaded. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
Michael Groeger wrote: m_returnData = Activator.CreateInstance(returnType, args) The documentation says the exception is thrown when the constructor throws an error; however I have found that it can be other things as well. When I wrote my "extend your .net programs at runtime with interfaces" article I was finding that sometimes I just needed to do a rebuild of all pieces of my project. It seems that the type the program expected was a different version than the one that was actually loaded. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
Hi James, I rebuild the solution, but the problem remains the same. But I found that there was an error in the constructor. I believed that I can break into the constructor from the call to CreateInstance() which was wrong. So I called New() directly and found the mistake. Thanks for your help! Michael