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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:16:02+00:00 2026-06-06T11:16:02+00:00

I have this anonymous type : var t= new {a=1,b=lalala,c=DateTime.Now}; How can I make

  • 0

I have this anonymous type :

var t= new {a=1,b="lalala",c=DateTime.Now};

How can I make it an array of Objects ( each element -> cast to object)

hence to something like :

object[]  v = new object[] {1,"lalala",DateTime.Now};

edit

p.s. this is just a knowledge question about learning to convert from 1 type to other.
i know i can initialize an array of object from the beginning. but this is a learning question.

sorry for not mentioned it.

order Is important…why? cause ConstructorInfo.Invoke is accepting

Type: System.Object[] An array of values that matches the number,
order (!!!) and type (under the constraints of the default binder) of the
parameters for this ….

  • 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-06T11:16:03+00:00Added an answer on June 6, 2026 at 11:16 am

    You’d have to use reflection, basically. It shouldn’t be too hard via Type.GetProperties, but I don’t know of anything “built-in”.

    As leppie pointed out, the ordering isn’t simple – you’d have to examine the order of the parameters, which would at least give you the order of all the types of the properties. If you only had different types, that would be fine.

    If you don’t care about the ordering, you can use:

    var array = t.GetType()
                 .GetProperties()
                 .Select(p => p.GetValue(t, null))
                 .ToArray();
    

    EDIT: I’ve just thought of something which will actually fix it, but it’s implementation specific. The C# compiler generates anonymous types using generic types. So new { A = 5, B = "foo" } will actually create an anonymous type like this:

    class <>_Anon<TA, TB>
    {
        internal <>_Anon(TA a, TB b)
    }
    

    so you can work out the property names in order based on the generic types of the generic properties, then fetch the properties in order from the concrete type. But it’s ugly…

    using System;
    using System.Linq;
    using System.Reflection;
    
    class Test    
    {
        // Note: this uses implementation details of anonymous
        // types, and is basically horrible.
        static object[] ConvertAnonymousType(object value)
        {
            // TODO: Validation that it's really an anonymous type
            Type type = value.GetType();
            var genericType = type.GetGenericTypeDefinition();
            var parameterTypes = genericType.GetConstructors()[0]
                                            .GetParameters()
                                            .Select(p => p.ParameterType)
                                            .ToList();
            var propertyNames = genericType.GetProperties()
                                           .OrderBy(p => parameterTypes.IndexOf(p.PropertyType))
                                           .Select(p => p.Name);
    
            return propertyNames.Select(name => type.GetProperty(name)
                                                    .GetValue(value, null))
                                .ToArray();
    
        }
    
        static void Main()
        {
            var value = new { A = "a", Z = 10, C = "c" };
            var array = ConvertAnonymousType(value);
            foreach (var item in array)
            {
                Console.WriteLine(item); // "a", 10, "c"
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of an anonymous type declared as: var list = new[]
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
I have an anonymous type of this form: new List<MyList>() { new Column {
I have this error: An anonymous type cannot have multiple properties with the same
I am new to C# 3.0 var type. Here I have a question about
Into some view data i have put the result of an anonymous type: var
We have this anonymous function in our code, which is part of the jQuery's
I need to fetch all the properties of an anonymous type which can be
I know I can't write a method like: public var MyMethod() { return new{
I have two objects defined something like this (simplified for sake of the question):

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.