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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:16:00+00:00 2026-06-13T18:16:00+00:00

I having trouble casting from a generic back to the original object in C#

  • 0

I having trouble casting from a generic back to the original object in C#

private static bool OpenForm<T>( )
{
    List<T> list = FormManager.GetListOfOpenForms<T>();
    if ( list.Count == 0 )
    {
        // not opened
        return false;
    }
    else
    {
        // opened
        foreach ( T f in list )
        {
            T ff = ( T ) Convert.ChangeType( f, typeof( T ) );

if I type ff. and intellisense pops up with the just a few methods and properties.

how can I have a variable here where it exposes all properties and methods of ff

        }
        return true;
    }
}
  • 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-13T18:16:01+00:00Added an answer on June 13, 2026 at 6:16 pm

    Since it is a generic method, T could literally be any type, down to a simple object. The compiler – and likewise the intellisense engine – has no idea what T is until runtime. Note that this is of course the behavior you want, and it is the reason you use generics in the first place. In this case, using static typing, there is no way to access the members of T outside of reflection.

    Now what I believe you are looking for is a constraint, that is to say that all Ts will always be of a certain base type. For example, if all Ts will be Forms, you can put a constraint on the method and then access the members of Form:

    private static bool OpenForm<T>() where T : Form
    {
        List<T> list = FormManager.GetListOfOpenForms<T>();
        if ( list.Count == 0 )
        {
            // not opened
            return false;
        }
        else
        {
            // opened
            foreach ( T f in list )
            {
                f.Text = "You found me!";
            }
        }
    }
    

    Note I have omitted your conversion from T f to T ff since it would do nothing.

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

Sidebar

Related Questions

I am having trouble casting an object to a generic IList. I have a
Having trouble getting the oauth access token and secret exchanged from the request tokens
Having trouble getting my POST arrays to show all checkbox values from my form.
I'm learning spring for android with androidannotations and I'm having trouble casting JSON data
Having trouble getting this syntax right: SELECT DISTINCT id FROM metadata WHERE (meta_key =
I'm having some trouble getting my head around casting an interface I've come up
I am having trouble casting one of my entities to IEntityWithRelationsships. I am using
I don't know if I'm missing something here, but I am having trouble casting
I'm having some trouble with the class casting in C++. To learn, I wanted
Having trouble using ajax to retrieve info from a database. I was getting the

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.