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

  • Home
  • SEARCH
  • 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 318463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:34:18+00:00 2026-05-12T08:34:18+00:00

See i have a situation like this… object myRoledata = List<Roles>() –> (some list

  • 0

See i have a situation like this…

object myRoledata  = List<Roles>() --> (some list or Ienumerable type)

Now i have a generic method which creates an XML object from List<T> –
Something like this..

public string GetXML<T>(object listdata)  
{  
    List<T> objLists = (List<T>)Convert.ChangeType(listData, typeof(List<T>));  
    foreach(var obj in listdata)   
    {  
        //logic to create xml  
    }  
}

Now in order to run this method I have to do like this:

string xml = GetXML<Roles>(myRoledata);

Now i dont know what Type may come to me to be passed to GetXML method. I have a method which will call GetXML for different Types e.g. Roles, Users etc

now i can get the Type within the List<> like this

Type genericType = obj.GetType().GetGenericArguments()[0];

but cannot pass it like this

string xml = GetXML<genericType>(myRoledata);

Is there anyway in which i can pass any genericTypes to GetXML method?

  • 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-12T08:34:19+00:00Added an answer on May 12, 2026 at 8:34 am

    This is a problem you probably want to avoid solving. It is possible, via reflection, to call methods dynamically without statically resolving them – but it kind of defeats the whole point of the type-annotations.

    Either do this:

    public string GetXML(IEnumerable listdata) {  
        foreach(object obj in listdata)   
            //logic to create xml  
    }
    

    … which you now can call with any IEnumerable, or write it the “modern” way as:

    public string GetXML(IEnumerable<object> listdata) {  
        foreach(object obj in listdata)   
            //logic to create xml  
    }
    

    … which you can call with any IEnumerable via GetXML(someEnumerable.Cast<object>()) and in C# 4.0 even directly by covariance.

    If you need the type of an element runtime, you can get it using .GetType() on each element, or you can just pass it in as a parameter (and provide an override for backwards-compatibility):

    public string GetXML(Type elementType, IEnumerable<object> listdata) {  
        foreach(object obj in listdata)   
            //logic to create xml  
    }
    
    public string GetXML<T>(IEnumerable<T> listdata) {
        return GetXML(typeof(T),listdata.Cast<object>());
    }
    

    Incidentally, if you’re constructing XML, a string is probably a less robust return-type choice: if possible, you could work with something like an XElement instead – and get xml-validity guarantee’s to boot.

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

Sidebar

Ask A Question

Stats

  • Questions 263k
  • Answers 263k
  • 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 In normal C you'd have to enclose this in brackets… May 13, 2026 at 11:58 am
  • Editorial Team
    Editorial Team added an answer Oie, Agamemon! I would encourage you to try a different… May 13, 2026 at 11:58 am
  • Editorial Team
    Editorial Team added an answer Actually Paul Beckingham's answer isn't quite correct. If you try… May 13, 2026 at 11:58 am

Related Questions

See i have a situation like this... object myRoledata = List<Roles>() --> (some list
I have a java program that runs a bunch of queries against an sql
I have a situation where I want my program to read in some numbers
I have a weird situation where I need to get the Name of the
OK, so I've got this totally rare an unique scenario of a load balanced

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.