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 6898773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:20+00:00 2026-05-27T07:20:20+00:00

Looking to cast an Object to a known type at runtime. I have a

  • 0

Looking to cast an Object to a known type at runtime. I have a class (call it Item for ease) which is the base class for Box. Box has it’s own properties as well as the ones from Item (obviously).

Basically I create an instance of Box using the CreateInstance method, this creates an Object of type object but the true type (as witnessed when doing ‘typeof’) is of type Box. I need to cast this Object back to Box without hard coding any switch / if etc. The code I have to test this is below and I’m running out of ideas.

//Base Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace Test11
{
    public class Item
    {
       public int property1 { get; set; }
       public int property2 { get; set; }
       public int property3 { get; set; }

    public Item()
    {
        property1 = 1;
        property2 = 2;
        property3 = 3;
    }
}

//Box Class - Inherits from Item
namespace Test11
{
    public class Box : Item
    {
        public int property4 { get; set; }

        public Box()
        {
            property4 = 4;
        }
    }
}

//Application Class
namespace Test11
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Item> BaseList = new List<Item>();
            object obj = Assembly.GetExecutingAssembly().CreateInstance("Test11.Box");
            Type t = Type.GetType("Test11.Box");

            //The following line does not work, need to make it work :)
            //BaseList.Add(obj as t); 
            Console.WriteLine(t.ToString());
            Console.ReadLine();
        }
    }
}

I’ve tried many different ways now, the one featured above is one of many. Any ideas or help out there?

  • 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-05-27T07:20:21+00:00Added an answer on May 27, 2026 at 7:20 am

    Your BaseList is expecting Item objects. You have to cast:

    if (obj is Item)
        BaseList.Add((Item)obj);
    

    Or:

    if (typeof(Item).IsAssignableFrom(t))
        BaseList.Add((Item)obj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom data type called StudentID , which has an implicit conversion
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
Looking for C# class which wraps calls to do the following: read and write
What .NET method has this error message: Object cannot be cast from DBNull to
When I am trying to cast Object obj to Type T , if it
I have a class which does operations on the database, but I want to
Looking for suggestions on file system management tools. We have several terabytes of images,
I'm working on a project in C# 4.0. I have several presenter classes, which
What I'm trying to do is send an object through Intents; I've been looking
I have a object graph that is setup like this Clients string Name List[Address]

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.