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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:38:27+00:00 2026-05-16T00:38:27+00:00

When I was looking to the System.Linq.Expression capabilities (creating expression trees in code, compiling

  • 0

When I was looking to the System.Linq.Expression capabilities (creating expression trees in code, compiling them, executing them) I was asking myself if this is possible for class creation as well – specifically generic classes.

I would expect something like Expression.Class() or Expression.GenericClass(). Looking at the methods I did not see any of those. It would be very practical, since I could build business objects dynamically – on the fly. Actually that is what I need. If there is another approach to that in .net c# that is also of interest to me.

  • 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-16T00:38:28+00:00Added an answer on May 16, 2026 at 12:38 am

    There is an example of how to do this in All-In-One Framework codeplex project
    http://1code.codeplex.com/

    // Define the assembly and the module.
    
    AppDomain appDomain = AppDomain.CurrentDomain;
    AssemblyName assemblyName = new AssemblyName("EmittedAssembly");
    AssemblyBuilder assembly = appDomain.DefineDynamicAssembly(
    assemblyName, AssemblyBuilderAccess.RunAndSave);
    
    // An assembly is made up of executable modules. For a single-module
    // assembly, the module name and file name are the same as the 
    // assembly name. 
    
    ModuleBuilder module = assembly.DefineDynamicModule(
    assemblyName.Name, assemblyName.Name + ".dll");
    
    
    /////////////////////////////////////////////////////////////////////
    // Declare the types (classes).
    // 
    
    // Declare the class "ClassA"
    TypeBuilder classA = module.DefineType("ClassA", TypeAttributes.Public);
    // Declare the class "ClassB"
    TypeBuilder classB = module.DefineType("ClassB", TypeAttributes.Public);
    
    // Define the fields stringField, classBField
    FieldBuilder stringField = classA.DefineField("stringField",
    typeof(string), FieldAttributes.Private);
    FieldBuilder classBField = classA.DefineField("classBField",
    classB, FieldAttributes.Public);
    
    /////////////////////////////////////////////////////////////////////
    // Define the property ClassBProperty
    PropertyBuilder classBProperty = classA.DefineProperty(
        "ClassBProperty", PropertyAttributes.None, classB, null);
    
    // The special set of attributes for the property set&get methods
    MethodAttributes getSetAttr = MethodAttributes.Public |
        MethodAttributes.SpecialName | MethodAttributes.HideBySig;
    
    // Define the "get" accessor method for ClassBProperty
    MethodBuilder classBGetProp = classA.DefineMethod(
        "get_ClassBProperty", getSetAttr, classB, Type.EmptyTypes);
    ILGenerator classBGetIL = classBGetProp.GetILGenerator();
    classBGetIL.Emit(OpCodes.Ldarg_0);
    classBGetIL.Emit(OpCodes.Ldfld, classBField);
    classBGetIL.Emit(OpCodes.Ret);
    
    // Define the "set" accessor method for ClassBProperty
    MethodBuilder classBSetProp = classA.DefineMethod(
        "set_ClassBProperty", getSetAttr, null, new Type[] { classB });
    ILGenerator sampleSetIL = classBSetProp.GetILGenerator();
    sampleSetIL.Emit(OpCodes.Ldarg_0);
    sampleSetIL.Emit(OpCodes.Ldarg_1);
    sampleSetIL.Emit(OpCodes.Stfld, classBField);
    sampleSetIL.Emit(OpCodes.Ret);
    
    // Map the get&set methods to PropertyBuilder
    classBProperty.SetGetMethod(classBGetProp);
    classBProperty.SetSetMethod(classBSetProp);
    
    /////////////////////////////////////////////////////////////////////
    // Define a method that uses the classBField
    MethodBuilder classAMethod = classA.DefineMethod("ClassAMethod", 
        MethodAttributes.Public);
    
    // Define the list generics and ienumerable generic
    Type listOf = typeof(List<>);
    Type enumOf = typeof(IEnumerable<>);
    Type listOfClassA = listOf.MakeGenericType(classA);
    Type enumOfClassA = enumOf.MakeGenericType(classA);
    
    // Define the method, ClassBMethod, for ClassB
    MethodBuilder classBMethod = classB.DefineMethod("ClassBMethod", 
        MethodAttributes.Public, typeof(void), new Type[] { listOfClassA });
    classBMethod.DefineParameter(1, ParameterAttributes.None, "list");
    
    // Write the body of ClassAMethod that calls ClassBMethod
    ILGenerator ilgenA = classAMethod.GetILGenerator();
    ilgenA.Emit(OpCodes.Nop);
    ilgenA.Emit(OpCodes.Ldarg_0);
    ilgenA.Emit(OpCodes.Ldfld, classBField);
    ilgenA.Emit(OpCodes.Ldnull);
    ilgenA.Emit(OpCodes.Callvirt, classBMethod);
    ilgenA.Emit(OpCodes.Ret);
    
    /////////////////////////////////////////////////////////////////////
    // Create the types.
    // 
    
    classA.CreateType();
    classB.CreateType();    
    
    /////////////////////////////////////////////////////////////////////
    // Save the assembly.
    // 
    
    assembly.Save(assemblyName.Name + ".dll");
    

    Sorry this is a bit long.. 😛

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

Sidebar

Ask A Question

Stats

  • Questions 533k
  • Answers 533k
  • 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 Please have a look at this article. It explains how… May 17, 2026 at 12:28 am
  • Editorial Team
    Editorial Team added an answer The reason this is happening is because Java is optimizing… May 17, 2026 at 12:28 am
  • Editorial Team
    Editorial Team added an answer The elements of the list collapse since you set float:left… May 17, 2026 at 12:28 am

Trending Tags

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

Top Members

Related Questions

I've been looking for this answer, and all I found was this link ,
I'm looking to add an advanced search capability to my ASP.NET/SQL Server 2005 application.
I am looking into strong typed Windows Forms databinding using extension methods. I have
What types and arguments does the method, Any when using Expression.Call take? I have
I have the following classes (I've trimmed the code): public class SqlWeightTrackerRepository : IWeightTrackerRepository
Given either of the variables below, how can I 'inspect' them to obtain the
I'm calling a UpdateUser on AuthenticationBase class in the System.ServiceModel.DomainServices.Server assembly installed by the
I am using LINQ select statement wrapped in a TransactionScope (to change the locking)
I'm using a shim property to make sure that the date is always UTC.
I have a couple of tables with a many-to-one relationship, and I'm trying to

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.