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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:07:46+00:00 2026-05-11T12:07:46+00:00

Is there a way to cast an instance of a class using a Type

  • 0

Is there a way to cast an instance of a class using a Type variable rather then an explicitly provided type?

For example in my method below ‘this’ is a derived type of ‘Node’. I want the method to repeatedly try to get a value from GetNodeIntrinsicProperty() after which if it get’s a null value it should cast itself as it’s base type and try again.

Basically, I want to call every implementation of GetNodeIntrinsicProperty() until I get a value.

        public string GetIntrinsicProperty(String propertyKey)     {         //sets the original type value         Type currType = this.GetType();          Node thisNode = this;         String propertyValue;          while (currType is Node)         {             //casts thisNode as CurrType             thisNode = thisNode as currType;              /*The live above gives me the following error              *               * Error    20  The type or namespace name 'currType' could not be found              (are you missing a using directive or an assembly reference?)   */                //trys to get the property with the current cast             //GetNodeIntrinsicProperty() is defined seperately in each type             propertyValue = thisNode.GetNodeIntrinsicProperty(propertyKey);              if (propertyValue != null)             {                 return propertyValue;             }              //sets CurrType to its base type             currType = currType.BaseType;         }          return null;     } 
  • 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. 2026-05-11T12:07:47+00:00Added an answer on May 11, 2026 at 12:07 pm

    Ok, so first the answer to your question.

    I’m assuming you have some structure like this:

        public class Node     {         public string GetIntrinsicProperty(String propertyKey)         {             //sets the original type value             Type currType = this.GetType();              Node thisNode = this;             String propertyValue;              while (currType.IsSubclassOf(typeof(Node)))             {                 MethodInfo mi = currType.GetMethod('GetIntrinsicProperty',BindingFlags.Instance | BindingFlags.Public,null,new Type[] {typeof(string)},null);                 if (mi.DeclaringType != typeof(Node))                 {                     propertyValue = (string)mi.Invoke(this, new object[] { propertyKey });                      if (propertyValue != null)                     {                         return propertyValue;                     }                 }                 //sets CurrType to its base type                 currType = currType.BaseType;             }             return null;         }     }      public class OtherNode : Node     {         new public string GetIntrinsicProperty(string propertyKey)         {             return 'OtherNode says Hi!';         }     }      public class TestNode : Node     {     } 

    The implementation of GetIntrinsicProperty above will do what you’re asking, but I would suggest that it’s wrong.

    You’re forcing a child class to exactly replicate your signature and a developer to understand what you want. This is what virtual methods are for. If I’m understanding you correctly the proper way to do what you want is this:

        public class Node     {         public virtual string GetIntrinsicProperty(String propertyKey)         {             switch(propertyKey)             {                 case 'NodeUnderstoodProp':                     return 'I know! Call on me!';                 default:                     return null;             }         }     }      public class OtherNode : Node     {         public override string GetIntrinsicProperty(string propertyKey)         {             switch (propertyKey)             {                 case 'OtherUnderstoodProp':                     return 'I'm the OtherNode, and I know better, call on me!';                 default:                     return base.GetIntrinsicProperty(propertyKey);             }         }     }      public class TestNode : Node     {     }       static void Main(string[] args)     {         Node node = new OtherNode();         var prop1 = node.GetIntrinsicProperty('NodeUnderstoodProp');         var prop2 = node.GetIntrinsicProperty('OtherUnderstoodProp');         var prop3 = node.GetIntrinsicProperty('PropTooHard!');          node = new TestNode();         prop1 = node.GetIntrinsicProperty('NodeUnderstoodProp');         prop2 = node.GetIntrinsicProperty('OtherUnderstoodProp');         prop3 = node.GetIntrinsicProperty('PropTooHard!');     } 

    The idea of virtual methods is that the type of your variable doesn’t determine which implementation is called, but rather the run time type of the object determines it.

    As far as I can tell, the situation you’re describing is one where you are trying do your own dispatch to the implementation of a method on the runtime type of the object. Pretty much the definition of a virtual method.

    If I didn’t get the question right, please clarify. 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • 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
  • added an answer I suspect that the Property Grid does not support your… May 11, 2026 at 3:53 pm
  • added an answer Not so familiar with iis7 but I can help you… May 11, 2026 at 3:53 pm
  • added an answer One word: ImageMagick. Not sure about if it generates the… May 11, 2026 at 3:52 pm

Related Questions

Is there a way to call method inside a running .NET assembly. The problem
Is there a way to Invoke an overloaded method using reflection in .NET (2.0).
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Kind of a random question... What I'm looking for is a way to express

Trending Tags

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

Top Members

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.