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 Activator.CreateInstance()

Using Activator.CreateInstance()

Scheduled Pinned Locked Moved C#
tutorialquestion
3 Posts 2 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
    Michael Groeger
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • M Michael Groeger

      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

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • J James T Johnson

        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

        M Offline
        M Offline
        Michael Groeger
        wrote on last edited by
        #3

        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

        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