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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:10:18+00:00 2026-05-11T16:10:18+00:00

I have a string serialization utility that takes a variable of (almost) any type

  • 0

I have a string serialization utility that takes a variable of (almost) any type and converts it into a string. Thus, for example, according to my convention, an integer value of 123 would be serialized as “i:3:123” (i=integer; 3=length of string; 123=value).

The utility handles all primitive type, as well as some non-generic collections, like ArrayLists and Hashtables. The interface is of the form

public static string StringSerialize(object o) {}

and internally I detect what type the object is and serialize it accordingly.

Now I want to upgrade my utility to handle generic collections. The funny thing is, I can’t find an appropriate function to detect that the object is a generic collection, and what types it contains – both of which pieces of information I need in order to serialize it correctly. To date I’ve been using coding of the form

if (o is int) {// do something}

but that doesn’t seem to work with generics.

What do you recommend?


EDIT: Thanks to Lucero, I’ve gotten closer to the answer, but I’m stuck at this little syntactical conundrum here:

if (t.IsGenericType) {
  if (typeof(List<>) == t.GetGenericTypeDefinition()) {
    Type lt = t.GetGenericArguments()[0];
    List<lt> x = (List<lt>)o;
    stringifyList(x);
  }
}

This code doesn’t compile, because “lt” is not allowed as the <T> argument of a List<> object. Why not? And what is the correct syntax?

  • 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-11T16:10:18+00:00Added an answer on May 11, 2026 at 4:10 pm

    Re your conundrum; I’m assuming stringifyList is a generic method? You would need to invoke it with reflection:

    MethodInfo method = typeof(SomeType).GetMethod("stringifyList")
                .MakeGenericMethod(lt).Invoke({target}, new object[] {o});
    

    where {target} is null for a static method, or this for an instance method on the current instance.

    Further – I wouldn’t assume that all collections are a: based on List<T>, b: generic types. The important thing is: do they implement IList<T> for some T?

    Here’s a complete example:

    using System;
    using System.Collections.Generic;
    static class Program {
        static Type GetListType(Type type) {
            foreach (Type intType in type.GetInterfaces()) {
                if (intType.IsGenericType
                    && intType.GetGenericTypeDefinition() == typeof(IList<>)) {
                    return intType.GetGenericArguments()[0];
                }
            }
            return null;
        }
        static void Main() {
            object o = new List<int> { 1, 2, 3, 4, 5 };
            Type t = o.GetType();
            Type lt = GetListType(t);
            if (lt != null) {
                typeof(Program).GetMethod("StringifyList")
                    .MakeGenericMethod(lt).Invoke(null,
                    new object[] { o });
            }
        }
        public static void StringifyList<T>(IList<T> list) {
            Console.WriteLine("Working with " + typeof(T).Name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 111k
  • Answers 111k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It will execute no sooner than 10 minutes from now… May 11, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer Put the select box in a form and make the… May 11, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer As long as the contract has call backs then no,… May 11, 2026 at 9:49 pm

Related Questions

I have a simple web method and ajax call and continue to recieve an
I have a collection of custom entity objects one property of which is an
Basically, I have a class with 2 methods: one to serialize an object into
I'm working on a project that has to connect to some ancient webservices that
I have a certain POJO which needs to be persisted on a database, current

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.