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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:02:53+00:00 2026-05-22T15:02:53+00:00

I have a method that returns byte[] but for now would like to avoid

  • 0

I have a method that returns byte[] but for now would like to avoid using generics on the method to supply the return type. I currently pass a parameter into this method that has the details of the type of the data that will be serialized to the byte[] return result.

Question:

How do I perform a dynamic cast of the result of .Invoke()?

I’m using reflection to call a method on a class the output of this is object and this result needs to be cast.

The Code:

public byte[] SomeMethod(/* some params */)
{
    MethodInfo info = /* ... */ .GetMethod(methodToCall);

    //Here I want to perform a dynamic cast:
    return (info.Invoke(classToCall, methodParams)).Serialize();

    //if I was to just have the method fixed to a type it would look like:
    return (info.Invoke(classToCall, methodParams) as SomeType).Serialize();

    //NOTE: .Serialize() is just my own extension method
}

What I have tried without success:

//with returnType as a string
var dynamicType = Type.GetType(returnType);

(info.Invoke(classToCall, methodParams) as dynamicType).Serialize();

Also some other variations with typeof(), GetTypeHandle, .ReflectedType


Am I missing something simple and/or obvious, or am I just trying to do something strange/silly?

Additional note: the SomeMethod is an override of a method of a class hence not wanting to have the method support a generic type.

  • 1 1 Answer
  • 3 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-22T15:02:54+00:00Added an answer on May 22, 2026 at 3:02 pm

    It is a common misunderstanding that the cast operator really does anything in this situation. You either want to convert from the type that Invoke returned to another type, or it already is the type you want it to be even though Invoke is declared to return object.

    Since I don’t think you want to convert anything, you have to first recognize that the return value of calling Invoke like this:

    var result = info.Invoke(classToCall, methodParams);
    

    the result will be a variable of type object but the value in that variable will already be the type you want to cast it to. You just don’t know exactly what it is. And you need to know what it is to call Serialize.

    Now, you have this extension method, or set of overloaded extension methods that operate on a compile time specified initial this parameter. One of them matches the type of the return value of the Invoke call.

    In order to choose which of these methods to call, you need to use reflection again to get a MethodInfo matching the right type and just call your Serialize method. It might help if it weren’t an extension method because now you have no easy way to look up the extension method the same way the compiler does at compile time only. Extension method matching is extremely laborious and not suited to dynamic invocation.

    Instead, if you want to use reflection a second time to call Serialize, without having a giant switch statement, then you need to have a utility class with all the Serialize methods as public overloads within that class. At least that is one way to approach the problem.

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

Sidebar

Related Questions

I am using java language,I have a method that is supposed to return an
If you have a method that returns an IEnumerable but inside returns an IQueryable
I'm using the Amazon SDK and I have a method that returns a Stream
I have class method that returns a list of employees that I can iterate
I have a data tier select method that returns a datatable. It's called from
I have a method in my Python code that returns a tuple - a
Suppose I have a method public Patient(int id) { ---- } that returns Patient
I have a subclass with an over-ridden method that I know always returns a
I have a method that can return either a single object or a collection
In Python compiled regex patterns have a findall method that does the following: Return

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.