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 error [modified]

Activator.CreateInstance error [modified]

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 1 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.
  • F Offline
    F Offline
    Fayu
    wrote on last edited by
    #1

    I am trying to create a class that is NOT abstract but is inherited from an abstract class. When trying to create this class using Activator.CreateInstance, I get an error saying: "Cannot create an abstract class". However, I am not tyring to create an instance of an abstract class rather a class that can be instanciated and is not abstract. See code below... Any ideas? Thanks in advance.

    namespace Test
    {
    [Serializable]
    public abstract class Cls1
    {
    ...
    public static Cls1 Open()
    {
    Cls1 provider = null;
    string typeName = "Test.Cls3";
    Type t = Type.GetType(typeName);
    provider = Activator.CreateInstance(t) as Cls1; // <----------- Error Here
    }
    }

    [Serializable]
    public abstract class Cls2 : Cls1
    {
    ...
    }

    [Serializable]
    public sealed class Cls3 : Cls2
    {
    ...
    }
    }

    -- Modified Wednesday, April 7, 2010 3:08 PM

    F 1 Reply Last reply
    0
    • F Fayu

      I am trying to create a class that is NOT abstract but is inherited from an abstract class. When trying to create this class using Activator.CreateInstance, I get an error saying: "Cannot create an abstract class". However, I am not tyring to create an instance of an abstract class rather a class that can be instanciated and is not abstract. See code below... Any ideas? Thanks in advance.

      namespace Test
      {
      [Serializable]
      public abstract class Cls1
      {
      ...
      public static Cls1 Open()
      {
      Cls1 provider = null;
      string typeName = "Test.Cls3";
      Type t = Type.GetType(typeName);
      provider = Activator.CreateInstance(t) as Cls1; // <----------- Error Here
      }
      }

      [Serializable]
      public abstract class Cls2 : Cls1
      {
      ...
      }

      [Serializable]
      public sealed class Cls3 : Cls2
      {
      ...
      }
      }

      -- Modified Wednesday, April 7, 2010 3:08 PM

      F Offline
      F Offline
      Fayu
      wrote on last edited by
      #2

      Seems to be a bug in VS2008 or could be my crappy Lenovo Laptop. Restarted computer and problem is gone.

      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