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 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

Related Questions

Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
I am looking for sample linq code snippet which uses System.Linq.Dynamic against a datatable.
Looking in this StackOverflow question it uses the following to send emails: System.Net.Mail.SmtpClient Is
I'm having a void in my Global.asax, looking like this using System; using System.Collections.Generic;
In looking at System.Linq.Enumerable through Reflector i noticed that default iterator used for Select
I'm looking to test system responsiveness etc. on a few machines under certain CPU
I'm looking content management system that would have features similar to stackoverflow: Users can
CompositionContainer.ComposePart is an extension method. why is that? Looking into the System.Componentmodel.Composition assembly, it
Im looking to have a login system on my asp.net mvc website and at
I'm looking to build a messaging system that handles conversation much like how Facebook

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.