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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:10:00+00:00 2026-05-14T05:10:00+00:00

This question is sort of a follow-up to my original question here . Let’s

  • 0

This question is sort of a follow-up to my original question here.

Let’s say that I have the following generic class (simplifying! ^_^):

class CasterClass<T> where T : class
{
    public CasterClass() { /* none */ }
    public T Cast(object obj)
    {
        return (obj as T);
    }
}

Which has the ability to cast an object into a specified type.

Unfortunately, at compile-time, I don’t have the luxury of knowing what types exactly I’ll have to work with, so I’ll have to instantiate this class via reflection, like so:

Type t = typeof(castedObject);

// creating the reflected Caster object
object CasterObj = Activator.CreateInstance(
    typeof(CasterClass<>).MakeGenericType(t)
);

// creating a reflection of the CasterClass' Cast method
MethodInfo mi = typeof(CasterClass<>).GetMethod("Cast");

Problem is, once I call the method using mi.Invoke(), it will return an object typed output, instead of the specifically-typed T instance (because of reflection).

Is there any way to have a method invoked through reflection return a dynamic type, as illustrated above? I’m pretty sure that .NET 3.5 doesn’t have the facilities to cast into a dynamic type (or rather, it would be very impractical).

Many thanks!

  • 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-14T05:10:01+00:00Added an answer on May 14, 2026 at 5:10 am

    If you have control over the classes you’ll be working with, have them all instantiate an interface that contains the methods you’ll be calling and once you instantiate, cast to the interface.

    I also posted an answer to your previous question with the same idea.

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

Sidebar

Related Questions

This is a related question , some sort of a follow up. Let's say
I know that this sort of question has been asked here before, but still
So this question is a sort of follow on from here ( how to
This is sort of a follow-up to my question here: Asp:Label is not shown
Follow up question to this: Linq Combine Left Join Data Say I have the
In a sort of follow up to this question , I have another problem
This question is a (sort of) follow-up to Intercepting/Rerouting TCP SYN packets to C++
This question is sort of a sequel to that question. When we want to
James here. This question relates (sort of) to the one I posted a little
My question is sort of a follow on from this question below but I

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.