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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:13:17+00:00 2026-05-16T14:13:17+00:00

I have dynamically created the type FruitTypes with these two properties private string _apple;

  • 0

I have dynamically created the type “FruitTypes” with these two properties

private string _apple;
public string Apple
{ 
    get { return _apple; } 
    set { _apple= value; } 
} 

private string _pear;
public string Pear
{ 
    get { return _pear; } 
    set { _pear= value; } 
}

Now the second type called “Farm” shall have two properties like this:

private string _ID;
public string ID
{ 
    get { return _ID; } 
    set { _ID= value; } 
} 

private ObservableCollection<FruitTypes> _fruits;
public ObservableCollection<FruitTypes> Fruits
{ 
    get { return _fruits; } 
    set { _fruits= value; } 
}

I have no idea how to create the Farm.
Can someone please help with with code samples?
Many Thanks,

UPDATE: I create the fruitTypes like this:

TypeBuilder typeBldr = modBldr.DefineType("FruitTypes", TypeAttributes.Public | TypeAttributes.Class);

FieldBuilder field = typeBldr.DefineField("_apple", typeof(string), FieldAttributes.Private);

PropertyBuilder propertyBuilder = typeBldr.DefineProperty("Apple", PropertyAttributes.None, typeof(string), new[] { typeof(string) });

MethodAttributes GetSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;

MethodBuilder currGetPropMthdBldr = typeBldr.DefineMethod("get_Apple", GetSetAttr, typeof(string), Type.EmptyTypes);

ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
currGetIL.Emit(OpCodes.Ldarg_0);
currGetIL.Emit(OpCodes.Ldfld, field);
currGetIL.Emit(OpCodes.Ret);

MethodBuilder currSetPropMthdBldr = typeBldr.DefineMethod("set_Apple", GetSetAttr, null, new[] { typeof(string) });
ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
currSetIL.Emit(OpCodes.Ldarg_0);
currSetIL.Emit(OpCodes.Ldarg_1);
currSetIL.Emit(OpCodes.Stfld, field);
currSetIL.Emit(OpCodes.Ret);

propertyBuilder.SetGetMethod(currGetPropMthdBldr);
propertyBuilder.SetSetMethod(currSetPropMthdBldr);

I do the same thing for a Pear Property within the same type.

Now how to wire them up is like this:

var tempName = new AssemblyName {Name = "MyTempAssembly"};
AssemblyBuilder assemBldr = AppDomain.CurrentDomain.DefineDynamicAssembly(tempName, AssemblyBuilderAccess.Run);
ModuleBuilder modBldr = assemBldr.DefineDynamicModule("MainMod");
Type generetedType = typeBldr.CreateType();

object generetedObject = Activator.CreateInstance(generetedType);
PropertyInfo[] properties = generetedType.GetProperties();

properties[0].SetValue(generetedObject , "Apple", null);
properties[1].SetValue(generetedObject , "Pear", null);
  • 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-16T14:13:17+00:00Added an answer on May 16, 2026 at 2:13 pm

    I assume your core issue is creating the field/property accessors in Farm as ObservableCollection<> with the generic type parameter set to the other dynamic type you just created? The key is to bake the type using TypeBuilder.CreateType() first; like this:

        AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("testassembly"), AssemblyBuilderAccess.Run);
    
        ModuleBuilder mb = ab.DefineDynamicModule("testmodule");
    
        TypeBuilder tbFoo = mb.DefineType("FooType");
        TypeBuilder tbBar = mb.DefineType("BarType");
    
        /* Make our List<FooType> type by baking tbFoo, then setting the result
         * as the generic type parameter for our List<>
         */
        Type tFoo = tbFoo.CreateType();
        Type genListType = typeof(List<>);
        Type listFooType = genListType.MakeGenericType(tFoo);
    
        /* Now we can define fields/properties of that type */
        FieldBuilder fb = tbBar.DefineField("_foolist", listFooType, FieldAttributes.Public);
    
        ConstructorInfo ciFooList = listFooType.GetConstructor(System.Type.EmptyTypes);
    
        ConstructorInfo ciObj = typeof(object).GetConstructor(System.Type.EmptyTypes);
    
        ConstructorBuilder cb = tbBar.DefineConstructor(MethodAttributes.Public|MethodAttributes.SpecialName|MethodAttributes.RTSpecialName, CallingConventions.Standard, System.Type.EmptyTypes);
        ILGenerator il = cb.GetILGenerator();
    
        /* Call the base object constructor */
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Call, ciObj);
    
        /* Set our _foolist_ field to a new List<FooType> */
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Newobj, ciFooList);
        il.Emit(OpCodes.Stfld, fb);
    
        /* Done! */
        il.Emit(OpCodes.Ret);
    
        /* Now we can bake and create a BarType with its public List<FooType> field
         * set to an empty list of FooTypes
         */
        Type tBar = tbBar.CreateType();
        object oBar = Activator.CreateInstance(tBar);
    

    I use List<T> for the example but it’s not much of a stretch to ObservableCollection<T>.

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

Sidebar

Related Questions

I have a table which is created dynamically. Sometimes it can have two columns
I have dynamically created popups that get created at run-time in the C# code
I have dynamically created the buttons in a div. And binding the click and
I have dynamically created some list of RadioButtons with some values. pro grammatically I
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I am going to make a GUI that will have dynamically created sets of
I have a dynamically created DataGridView that has a valid DataSource with one row
I have a dynamically created list. Items from the list should be able to
I have a dynamically created table which in the last <td> there is a
I have a dynamically created GirdView based on value of a DropDownList. I use

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.