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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:28:22+00:00 2026-05-16T22:28:22+00:00

In C# 3.0 you can use Expression to create a class with the following

  • 0

In C# 3.0 you can use Expression to create a class with the following syntax:

var exp = Expression.New(typeof(MyClass));
var lambda = LambdaExpression.Lambda(exp);
object myObj = lambda.Compile().DynamicInvoke();

But how do you use Expression to create an Anonymous class?

//anonymousType = typeof(new{ Name="abc", Num=123});
Type anonymousType = Expression.NewAnonymousType???  <--How to do ?
var exp = Expression.New(anonymousType);
var lambda = LambdaExpression.Lambda(exp);
object myObj = lambda.Compile().DynamicInvoke();
  • 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-16T22:28:22+00:00Added an answer on May 16, 2026 at 10:28 pm

    You’re close, but you have to be aware that anonymous types don’t have default constructors. The following code prints { Name = def, Num = 456 }:

    Type anonType = new { Name = "abc", Num = 123 }.GetType();
    var exp = Expression.New(
                anonType.GetConstructor(new[] { typeof(string), typeof(int) }),
                Expression.Constant("def"),
                Expression.Constant(456));
    var lambda = LambdaExpression.Lambda(exp);
    object myObj = lambda.Compile().DynamicInvoke();
    Console.WriteLine(myObj);
    

    If you don’t have to create many instances of this type, Activator.CreateInstance will do just as well (it’s faster for a few instances, but slower for many). This code prints { Name = ghi, Num = 789 }:

    Type anonType = new { Name = "abc", Num = 123 }.GetType();
    object myObj = Activator.CreateInstance(anonType, "ghi", 789);
    Console.WriteLine(myObj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I create a dynamic lambda expression to pass to use in my
I am trying to create Linq Expression that I can use to query child
I am trying to use regex generators to create an expression, but I can't
in C# I can use var myUser = New User {FirstName = John, LastName
I'm trying to create a simple qDebug-like class I can use to output debug
How do you create a lambda expression for a function that can handle unknown
I'm trying to find a single regular expression that I can use to parse
What regular expression can I use (if any) to validate that a given string
I can't use TryGetValue() from dictionary in linq expression with anonymous type. Dictionary<string, string>
Can you use a lambda expressions as an argument to an attribute? The motivation

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.