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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:08:55+00:00 2026-06-09T14:08:55+00:00

What I am trying to achieve is a generic way to use the add-method

  • 0

What I am trying to achieve is a generic way to use the add-method on a parent entity to add all the children of that entity type. This must be done to “chain” the entity objects together so entity framework can do its “magic” with the database keys (they are all identity columns).

This is the kind of code that I use today:

EntityObject ParentEntityObject;
EntityObject ChildEntityObject;
((Application)ParentEntityObject).MessageLists.Add((MessageList)ChildEntityObject);

I have “Application” and “MessageList” as strings in my program. I would therefore like to do two things:

  1. Use GetType with the strings so ParentEntityObject and ChildEntityObject could be dynamically casted.
  2. Use some kind of reflection magic to get the ‘MessageLists.Add’ function. Again; let’s assume that I have ‘MessageLists’ as a string.

I am unsure of how this could be done. Any advice is greatly appreciated!

  • 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-09T14:08:56+00:00Added an answer on June 9, 2026 at 2:08 pm

    You can try expression trees to do the “magic” stuff.
    For example:

    class BaseEntity
    {
    
    }
    class MessageList : BaseEntity
    {
    
    }
    class Application:BaseEntity
    {
        public List<BaseEntity> MessageLists { get; set; }
        public Application()
        {
            MessageLists = new List<BaseEntity>();
        }
    }
    class ExpressionHelper
    {
        public static Action<object,object> GetMethod(string targetTypename,string targetMemberName,string sourceTypeName)
        {
            ParameterExpression targetExpression = Expression.Parameter(typeof(object));
            MemberExpression propertyExpression = Expression.PropertyOrField(Expression.TypeAs(targetExpression, Type.GetType(targetTypename)), targetMemberName);
            ParameterExpression sourceExpression = Expression.Parameter(typeof(object));
            Expression callExpression = Expression.Call(propertyExpression, "Add", null, Expression.TypeAs(sourceExpression, Type.GetType(sourceTypeName)));
    
            var lambda = Expression.Lambda<Action<object, object>>(callExpression, targetExpression, sourceExpression);
            var method = lambda.Compile();
            return method;
        }
    }
    

    Usage:

        var app = new Application();
        var messageList = new MessageList();
        var magicMethod = ExpressionHelper.GetMethod(/*type*/"Application",/*name of Property-Storage*/"MessageLists",/*type of element to store*/"MessageList");
        magicMethod(app, messageList);
        //now app.MessageLists has one element
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to achieve two-way binding between a DataGridView and a BindingList that provides
I am trying to achieve something like this. The Expandable List consists of the
I am trying to achieve something in MySQL but even with all the answers
I am trying to achieve dynamic META information so that it will allow META
This is a scenario created to help understand what Im trying to achieve. I
I am trying to write a recursive C# function that will operate on generic
So I'm trying to create a generic select by ID method for a base
I'm trying to implement a Generic Repository. This is what I've got so far
How can I make a generic helper method to convert the type used by
I'm trying to figure a way to create a generic class for number types

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.