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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:20:02+00:00 2026-05-20T14:20:02+00:00

I am trying to make a generic method that can accept any object, in

  • 0

I am trying to make a generic method that can accept any object, in which, even the object is List<AnyClass>.
Something like:

public static void MyMethod(object _anyObject)
{

}

So in my method it accept the parameter as Object, then I will determine what to do by their type to do

So when I know that’s a List type, I want to convert that Object back to List<AnyClass>, iternate each object and their propertie

I tried to do:

List<object> ObjectList = object as List<object>;

But it will return null, because the object originally is List<AnyClass>

Thanks in advance

================

Edit: Sorry, seems like I haven’t make it clear, because I simplifed the problem…
I means the Object might be:

_anyObject = List<Class1>
_anyObject = Class2
_anyObject = DataSet
_anyObject = AnyClass
_anyObject = List<object>

So say, the user can even put in a List<object>, but each of the object in that list can be different class..That’s why I can’t use <T>.

The original of my problem is:

public static void MyMethod(List<object> _anyList)
{

}

Then I don’t care what they put into the list, even the object in the list contains another object list…

  • 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-20T14:20:03+00:00Added an answer on May 20, 2026 at 2:20 pm

    The generic List class implements the non-generic IList interface, so you can say:

    IList objectList = obj as IList;
    

    However, what you’re trying to do is generally considered bad practice as it eliminates any semblance of type safety. If you explain in what context you’re using this, we might be able to suggest a better alternative, like using a generic method.

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

Sidebar

Related Questions

I'm trying to make a genric code to serialize and deserialize any object that
I have a page that I am trying to make a generic style of
I'm trying to make this code generic: public final Object unwrap(Class arg0) { throw
I know I can create a generic method that I can call from other
I want to create a helper method that can take in ANY type of
As an exercise I'm trying to create a function that returns a generic list
I am trying to make a generic Duplicate linq extension method. But I cannot
I'm trying to make a deep copy of a generic list, and am wondering
I'm having a brain fart trying to make the following method more generic such
I'm trying to implement the generic method which is intended for converting objects of

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.