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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:39:57+00:00 2026-06-15T23:39:57+00:00

I have a problem with downcasting, how do I convert the string Label to

  • 0

I have a problem with downcasting, how do I convert the string “Label” to the class type Label for casting and is it possible to reach the ‘Texts’ class?

public class GFXObject
{
   // Constructor etc...
}
public class Label : GFXObject
{
    public Texts Text = new TextClass();

    // Constructor etc...
}

public class Button : GFXObject
{
    // Constructor etc...
}

public class Texts
{
    public string Text = "empty";

    // Constructor etc...
}

// My List of objects
Dictionary<string, GFXObject> objects;

// Disregarding any troubleshooting
public void ChangeText(string classtype, string name, string text)
{
    // How do I convert the string classtype to a ClassType
    ((classtype)objects[name]).Text.Text = text;

    // If parameters would be equal to "Label", "label", "changed":
    // ((Label)objects["label"].Text.Text = "changed";
}

Is it possible to use the ChangeText function if extended or modified?

  • 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-15T23:39:57+00:00Added an answer on June 15, 2026 at 11:39 pm

    You can do that only via reflection:

    public void ChangeText(string classtype, string name, string text)
    {
        var type = Type.GetType(classtype);
        type.GetProperty("Text").SetValue(objects[name], text);
    }
    

    Please note that classtype needs to be an assembly-qualified name if the method is in a different assembly than the type. If it is in the same assembly it needs to be fully qualified, i.e. including the namespace.

    If that’s a problem, you could get rid of the classtype parameter and use this method:

    public void ChangeText(string name, string text)
    {
        var type = objects[name].GetType();
        type.GetProperty("Text").SetValue(objects[name], text);
    }
    

    Another possibility would be to make use of the DLR and the dynamic keyword:

    public void ChangeText(string name, string text)
    {
        dynamic item = objects[name];
        item.Text = text;
    }
    

    Please note that every code that uses the dynamic item variable is evaluated only at runtime. This means that this code compiles even if there is no Text property on that particular item. It will then throw an exception at runtime.


    Having said all that, the correct solution to this problem would be to introduce a base class or interface that holds the Text property.

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

Sidebar

Related Questions

I have Value-Objects/Beans (only containing members, no logic): public class Parent { String first;
I have problem with context menu. I have got: @Override public void onCreateContextMenu(ContextMenu menu,
I have 2 classes Test(Base)and Program(child). Now I am facing some problem while downcasting.
I have problem with encoding data in Oracle database. I want to xor string
i have problem with enum I need make a enum in base class or
I have problem with deserialize document to object using XmlSerializer class. Code my function
I have problem to find a good namespace design and class order. Imagine we
I have problem with binding to static property. I want to have Label with
I'm handling a problem in downcasting a query.uniqueResult() using hibernate. I have 2 classes:
I have problem in C#. I have a list box and a class called

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.