Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9261971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:14:40+00:00 2026-06-18T13:14:40+00:00

I have this base class: namespace DynamicGunsGallery { public class Module { protected string

  • 0

I have this base class:

namespace DynamicGunsGallery
{
    public class Module
    {
        protected string name;

        public virtual string GetName() { return name; }
        public virtual string GetInfo() { return null;  }
    }
}

And Im creating dynamic libraries that inherit from base class, e.g. (AK47.dll)

namespace DynamicGunsGallery
{
    public class AK47 : Module
    {
        public AK47() { name = "AK47";  }

        public override string GetInfo()
        { 
            return @"The AK-47 is a selective-fire, gas-operated 7.62×39mm assault rifle, first developed in the USSR by Mikhail Kalashnikov.
                    It is officially known as Avtomat Kalashnikova . It is also known as a Kalashnikov, an AK, or in Russian slang, Kalash.";
        }
    }
}

I’m loading dynamic libraries using this (inspired by this link):

namespace DynamicGunsGallery
{
    public static class ModulesManager
    {
        public static Module getInstance(String fileName)
        {
            /* Load in the assembly. */
            Assembly moduleAssembly = Assembly.LoadFile(fileName);

            /* Get the types of classes that are in this assembly. */
            Type[] types = moduleAssembly.GetTypes();

            /* Loop through the types in the assembly until we find
             * a class that implements a Module.
             */
            foreach (Type type in types)
            {
                if (type.BaseType.FullName == "DynamicGunsGallery.Module")
                {
                    //
                    // Exception throwing on next line !
                    //
                    return (Module)Activator.CreateInstance(type);
                }
            }

            return null;
        }
    }
}

I have included base class in both, my executable which contains ModuleManager and in dll library. I have no problem when compiling, but when I run this code I get an error:

InvalidCastException was unhandled.

Unable to cast object of type DynamicGunsGallery.AK47 to type
DynamicGunsGallery.Module

So the question is: Why cant I cast derivated class to base class?

Is there other way to load dynamic library and “control” it using methods from base class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T13:14:41+00:00Added an answer on June 18, 2026 at 1:14 pm

    Going from your comments:

    In your sub library, you can’t re-declare module; you must reference the module from the original library.

    Add a reference to the main project from the project which has ak class in it.

    I’d also think about changing the namespaces to make it obvious that you’ve got two libraries going on

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class
I have a base class like this: public class BaseResponse { public string ErrorMessage
I have code like this: namespace N { class B { public: virtual void
I have this class public class Address:Entity { public virtual string Address1 { get;
This won't compile: namespace Constructor0Args { class Base { public Base(int x) { }
I have this classes on the same namespace: public partial class BaseForm : Form
I have this model namespace ProjectTimer.Models { public class TimerContext : DbContext { public
#include<iostream> using namespace std; class base { protected: int a; public: base(int i) {
I have a base interface class: class A { public: ITask(){} virtual bool Start()=0;
I have a custom WPF control base class. This base class registers a custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.