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

Activator.CreateInstance

Scheduled Pinned Locked Moved C#
sysadminregexhelpquestion
6 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.
  • S Offline
    S Offline
    scotlandc
    wrote on last edited by
    #1

    Hello. Am not sure about this, hope someone can provide a little help. I've got a folder with a variety of pre-defined Crystal Reports which will need to be instantiated depending on some user input. This folder is not part of the solution, it's just sitting on a server drive. So assuming the input is rptTest, I need to instantiate the report called rptTest. If the app can't find a match, it alerts the user. Is it Activator.CreateInstance I need to accomplish this or is it simply not possible? If it is, can anyone provide a code sample. I know I need an assemblyname and a typename for one but which would be the user input and what would the other be? Hope I'm being clear and that someone can help. Thanks in advance

    B P 2 Replies Last reply
    0
    • S scotlandc

      Hello. Am not sure about this, hope someone can provide a little help. I've got a folder with a variety of pre-defined Crystal Reports which will need to be instantiated depending on some user input. This folder is not part of the solution, it's just sitting on a server drive. So assuming the input is rptTest, I need to instantiate the report called rptTest. If the app can't find a match, it alerts the user. Is it Activator.CreateInstance I need to accomplish this or is it simply not possible? If it is, can anyone provide a code sample. I know I need an assemblyname and a typename for one but which would be the user input and what would the other be? Hope I'm being clear and that someone can help. Thanks in advance

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Activator.CreateInstance is used to create instances of C#/.Net types at runtime. This allows you to switch which class gets created based on runtime information (user input or information provided by virtual methods, for example).

      abstract class A {}
      class B : A {}
      class C : A {}

      A CreateSomething(string classname){
      return Activator.CreateInstance(Type.GetType(classname));
      }

      A CreateSomethingOfTheSameType(A template){
      return Activator.CreateInstance(template.GetType());
      }

      I don't believe Crystal Reports are saved as .Net classes and thus this won't be the correct approach. I would imagine that Crystal allows you to load reports from file.

      1 Reply Last reply
      0
      • S scotlandc

        Hello. Am not sure about this, hope someone can provide a little help. I've got a folder with a variety of pre-defined Crystal Reports which will need to be instantiated depending on some user input. This folder is not part of the solution, it's just sitting on a server drive. So assuming the input is rptTest, I need to instantiate the report called rptTest. If the app can't find a match, it alerts the user. Is it Activator.CreateInstance I need to accomplish this or is it simply not possible? If it is, can anyone provide a code sample. I know I need an assemblyname and a typename for one but which would be the user input and what would the other be? Hope I'm being clear and that someone can help. Thanks in advance

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

        Here's what I use:

                System.IO.FileInfo Defile
        

        ...
        CrystalDecisions.CrystalReports.Engine.ReportDocument crRpt =
        new CrystalDecisions.CrystalReports.Engine.ReportDocument() ;
        crRpt.Load ( Defile.FullName ) ;

        S 1 Reply Last reply
        0
        • P PIEBALDconsult

          Here's what I use:

                  System.IO.FileInfo Defile
          

          ...
          CrystalDecisions.CrystalReports.Engine.ReportDocument crRpt =
          new CrystalDecisions.CrystalReports.Engine.ReportDocument() ;
          crRpt.Load ( Defile.FullName ) ;

          S Offline
          S Offline
          scotlandc
          wrote on last edited by
          #4

          Thanks for the answers guys, much appreciated. Just one more thing, is there a way I can instantiate a custom dataset on the fly as well. Similar to the crystal reports, I've another folder which contains XSDs to use as the datasources for the reports. Could they also be generated according to user input? Thanks again for your help so far.

          B 1 Reply Last reply
          0
          • S scotlandc

            Thanks for the answers guys, much appreciated. Just one more thing, is there a way I can instantiate a custom dataset on the fly as well. Similar to the crystal reports, I've another folder which contains XSDs to use as the datasources for the reports. Could they also be generated according to user input? Thanks again for your help so far.

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            You can construct a DataSet manually at runtime (adding tables, columns, rows etc), or you can use DataSet.ReadXml.

            S 1 Reply Last reply
            0
            • B BobJanova

              You can construct a DataSet manually at runtime (adding tables, columns, rows etc), or you can use DataSet.ReadXml.

              S Offline
              S Offline
              scotlandc
              wrote on last edited by
              #6

              You, Sir, are a very good man. I forgot totally about calling the ReadXml method, saves me from having to turn everything into an XSD and can simply populate the crystal report using it instead. Bob for President.

              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