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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:35:28+00:00 2026-06-12T23:35:28+00:00

Is there way to typecast an object to some specific type at runtime? Is

  • 0

Is there way to typecast an object to some specific type at runtime? Is it possible at all?

public static void TryTypeCasting(object obj)
{
    Type type = obj.GetType();
    // Can I use this "type" variable somehow to typecast "obj" to its actual type?
}

I am using C# 4.0.


EDIT 1:

Thanks all for your inputs.

I may be trying to achieve something impossible. But I posted this question in order to get views of experts on this and to know if something like this is made achievable in C# 4.0.

Here is a real time problem:

In our product, our client side API (method) serializes instance of “some” class (say Employee) that derives from our entity class called Person. That serialized instance (i.e. string value) is sent to the server side API (a method which has responsibility to de-serialize string to appropriate class’s instance) through some intermediate classes. So, on the server side, only thing that API gets is a string.

However while serializing, custom serializer always add fully qualified name of the class (whose instance is being serialized) as the first line of the resulting output. So on server side, while reading the first line, I know the class (i.e. Employee in this case) to which the string should be de-serialized.

Further, we call a web service method (which I am not allowed to change) that accepts an argument of type Person.

Now, after deserialization at this stage, I have an instance of Employee stored in a variable of type object. But even though instance is available, I cannot pass it as an argument until I typecast it to Employee. How can I achieve this?

Sample code is provided here:

 public static void Deserialize(string serializedObject)
        {
            StringReader stringReader = new StringReader(serializedObject);

            // Read the first line to know class and assembly details
            string firstLine = stringReader.ReadLine();
            string[] assemblyAndClassDetails = firstLine.Split(new[] { ',' }, StringSplitOptions.None);
            string className = assemblyAndClassDetails[0];
            string assemblyName = assemblyAndClassDetails[1];

            // Remove the first line before passing it to the serializer
            serializedObject = serializedObject.Remove(0, firstLine.Length);

            // Know the type of the serialized instance
            Type typeToBeDeserializedTo = Type.GetType(className);

            DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeToBeDeserializedTo);
            using(MemoryStream memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(serializedObject)))
            {
                memoryStream.Position = 0;
                object deserializedObject = dataContractJsonSerializer.ReadObject(memoryStream);

                // NOW I WANT TO call a method that accepts an argument of type `Person` How can I do this?
            }
        }
  • 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-12T23:35:30+00:00Added an answer on June 12, 2026 at 11:35 pm

    No, this is entirely impossible (unless of course the specific type is known at compile time, in which case you can e.g. hardcode a cast).

    It could never be any other way, since type casting means that the compiler has full knowledge of what the type of the result is. How would it be possible for the compiler to have knowledge of something that can only be determined at runtime?

    Whenever this question comes up (and it does every so often), the answer is “there is probably an appropriate solution for your situation that involve neither this hypothetical type of cast nor reflection”. If you state your case in more detail we could suggest such a solution.

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

Sidebar

Related Questions

Is there a way to typecast from Java.lang.Object to an instance of a custom
Is there any shortcut or better way to typecast List<bool> to List<object> ? I
Is there a way to typecast String type to JLayeredPane ? Following is the
Is there a safer way to typecast data from a generic pointer.? More specifically
Is there a way to automatically typecast values that are stored using ActiveRecord::Base.store? Take
I am receiving some JSON object from the server, and I want to 'typecast'
Is there way to get file from windows xp command prompt? I tried to
Is there way to automatically maximize the output window on hitting build and then
Is there way to copy a file into Plone with WebDAV and have Plone
is there way how to get name ov event from Lambda expression like with

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.