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

  • Home
  • SEARCH
  • 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 6130623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:52:47+00:00 2026-05-23T16:52:47+00:00

EDIT: changed Activator , still doesn’t work. So I’m pretty (very) new to C#

  • 0

EDIT: changed Activator, still doesn’t work.

So I’m pretty (very) new to C# and I’m pretty sure this is a dupe, but I’ve looked through the previous questions and I still can’t work out all the points.

I am trying to reduce code smell by replacing some repeated code with a map over a generic list. Specifically, I have code that looks like

var fooNode = node as IFoo;
var barNode = node as IBar;
var bazNode = node as IBaz;
...

if(fooNode != null)
    return new FooThing();
if(barNode != null)
    return new BarThing();
if(bazNode != null)
    return new BazThing();
...

and I want to generalise it.


Here’s my attempt:

var types = new Dictionary<Type, Type>
{
    {typeof(IFoo), typeof(FooThing)},
    {typeof(IBar), typeof(BarThing)},
    ...
}

foreach(var entry in types)
{
    var castNode = node as entry.Key;
    return Activator.CreateInstance(entry.Value);
}

Naturally, it doesn’t work: The type or namespace name 'entry' could not be found (are you missing a using directive or an assembly reference?). Can you help? Is this sort of thing even possible in C#?

  • 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-23T16:52:48+00:00Added an answer on May 23, 2026 at 4:52 pm

    How about this?

    foreach(var entry in types)
    {
        if (node != null && entry.Key.IsAssignableFrom(node.GetType()))
        {
            return Activator.CreateInstance(entry.Value);
        }
    }
    

    The problem is that you are confusing generic type parameters with runtime types and in particular the Type class.

    If you know what a type will be at compile time then you can use the generic Activator.CreateInstance<T>() method to create an instance of the underlying object – you can use things like type parameters so that this line of code doesn’t need to know what the type is, for example:

    T CreateObject<T>()
    {
        return Activator.CreateInstance<T>();
    }
    

    However this just passes the buck. In order to call this method the value of the type parameter T must be supplied somewhere – either way the compiler must be able to resolve T to a type (rather than a variable or method).

    Conversely the Type class encodes type information at runtime such as its name or the assembly that a type is declared in. Activator.CreateInstance also comes with an overload that allows you to supply an instance of Type:

    object CreateObject(Type type)
    {
        return Activator.CreateInstance(type);
    }
    

    In your case it looks like you don’t know what the types will be at compile time, so you will be mostly working with the Type class – you can use typeof(MyClass) to get an instance of the the corresponding Type for a class known at runtime, and myObject.GetType() to get type information for an object at runtime.

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

Sidebar

Related Questions

EDIT: changed the title. I didn't know it at the time but this is
EDIT: This question was originally about checkboxes, but I am getting the same behavior
I have the following very simple application that compiles and runs fine: EDIT: changed
[EDIT] This question is how do I do atomic changes to entity beans with
And why don't they change it? Edit: The reason ask is because I'm new
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
So I just created a textbox with JavaScript like this: EDIT: Added the len
This is the most bizarre question I've ever asked. I'm not even sure how

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.