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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:48:08+00:00 2026-05-15T18:48:08+00:00

I have a base class Element and derived two other classes from it, Solid

  • 0

I have a base class “Element” and derived two other classes from it, “Solid” and “Fluid”.

    class Element
    {...}

    class Solid : Element
    {
        public const int ElemType = 2;
        ...
    }

    class Fluid : Element
    {
        public const int ElemType = 3;
        ...
    }

I have made an instance of “Element” class, “E1”. The following method is supposed to get an integer “ElType” and “E1” as its arguments and assign to “E1” as “Solid” or “Fluid” Element (or any other “Element” which will be introduced later). I mean, “E1” should be assigned to a “Solid” type if “IElType == 2” and “Fluid” type if “IElType == 3” and …. I wanted to make it possible for my colleagues to derive as many classes as they want, from “Element” class, and be sure that only by setting a proper value for “ElemType” the program will recognize their “Element”s.

    private static void ElemInitializer(int ElType, out Element E1)
    {
        E1 = new Element();
        Type T1 = typeof(Element);
        Type[] T2 = Assembly.GetAssembly(T1).GetTypes();
        List<Type> T3=new List<Type>();
        foreach (Type t1 in T2)
        {
            if (t1.IsSubclassOf(T1))
            {
                MemberInfo[] M1 = t1.GetMembers();
                foreach (MemberInfo m1 in M1)
                {
                    if (m1.Name == "ElemType")
                    {
                        FieldInfo F1 = t1.GetField("ElemType");
                        int int1 = (int)F1.GetValue(t1);
                        if (int1 == ElType)
                        {
                            // Here I want to assign to E1 as t1 type. Such as:
                            // E1 = new t1(); Of course this is wrong!
                        }
                    }
                }
            }
        }
    }

Here is the problem , I want “E1” be of type “t1”, but it is not possible to do this by a simple code like this:

E1 = new t1(); //Of course this is wrong!

Is there any way to assign to “E1” as “t1” type? Sorry for nontechnical English.

  • 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-15T18:48:08+00:00Added an answer on May 15, 2026 at 6:48 pm

    First of all, use a better naming convention, it makes it easier to help you and it will help your self in the long run too. The below code should do it

    private static void ElemInitializer(int ElType, out Element E1)
    {
        E1 = new Element();
        Type typeofElement = typeof(Element);
        Type[] assemblyTypes = Assembly.GetAssembly(T1).GetTypes();
        foreach (Type elementType in assemblyTypes )
        {
          if (elementType.IsSubclassOf(typeofElement))
          {
            FieldInfo field = elementType.GetField("ElemType");
            int elementId = (int)field.GetValue(elementType);
            if (elementId == ElType)
            {
               E1 = (Element)Activator.CreateInstance(elementType);
               return;
            }
          }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After the call to alloc init, the retain count on… May 16, 2026 at 10:41 am
  • Editorial Team
    Editorial Team added an answer @request.env["HTTP_REFERER"] = :index Testing Guide 4.4 Instance Variables Available You… May 16, 2026 at 10:41 am
  • Editorial Team
    Editorial Team added an answer $this->_table->select()->orWhere($condition); To build more complexe queries (i.g. sub-conditions) you might… May 16, 2026 at 10:41 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a tempated base class check and publically derived class childcheck. the base
I have a class derived from Dictionary. I need this class to simulate a
My NSArray holds two different types of classes, both of which derive from NSObject
I have a code: public class Layout : UserControl { protected void DisplayX_DisplayClicked(object sender,
I have this base class package sevengames.miranda.front.res { import flash.display.MovieClip; import flash.text.TextField; public class
At work we have a base class, let's call it IntrusiveBase, that acts something
How do I bind an element of a derived <UserControl> to an element of
I'm writing a plug-in for a 3D modeling program. I have a custom class
Yes, I'm new to c#! :) i'm using .Net4 VS2010. I have Three classes
I am learning Fluent NHibernate and this issue arose from that project. I have

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.