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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:14:16+00:00 2026-05-30T17:14:16+00:00

I create a generic method: private void UpdateAllProperties<idType, entityType>(entityType currentEntity, entityType newEntity) where idType

  • 0

I create a generic method:

private void UpdateAllProperties<idType, entityType>(entityType currentEntity, entityType newEntity)
    where idType : IEquatable<idType>
    where entityType : AbstractEntity<idType>
{
    var currentEntityProperties = currentEntity.GetType().GetProperties();
    var newEntityProperties = newEntity.GetType().GetProperties();

    foreach (var currentEntityProperty in currentEntityProperties)
    {
        foreach (var newEntityProperty in newEntityProperties)
        {
            if (newEntityProperty.Name == currentEntityProperty.Name)
            {
                if (currentEntityProperty.PropertyType == typeof(AbstractEntity<>))
                {
                    // Here i want to use this method again, but i need to inform the types.. how can i do anything like that:
                    var idPropertyType = currentEntityProperty.PropertyType.GetProperty("Id").GetType();
                    var entityPropertyType = currentEntityProperty.PropertyType;

                    // Here i got the error because i cannot set through this way
                    this.UpdateAllProperties<idPropertyType, entityPropertyType>(currentEntityProperty.GetValue(currentEntity, null), newEntityProperty.GetValue(newEntity, null));

                    break;
                }
                else if (currentEntityProperty.PropertyType == typeof(ICollection<>))
                {
                    // TODO

                    break;
                }
                else
                {
                    currentEntityProperty.SetValue(currentEntity, newEntityProperty.GetValue(newEntity, null), null);

                    break;
                }
            }
        }
    }
}

How can i do this?

  • 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-30T17:14:17+00:00Added an answer on May 30, 2026 at 5:14 pm

    You will have to make the call using Type.GetMethod() to get the method and MethodInfo.MakeGenericMethod() to set it up with the right types, before using MethodInfo.Invoke() to call the method.

    Something like:

    var idPropertyType = currentEntityProperty.PropertyType.GetProperty("Id").PropertyType;
    var entityPropertyType = currentEntityProperty.PropertyType;
    
    var method = this.GetType().GetMethod("UpdateAllProperties", 
                                  BindingFlags.Instance | BindingFlags.NonPublic);
    var genericMethod = method.MakeGenericMethod(idPropertyType, entityPropertyType);
    genericMethod.Invoke(this, new[] { 
        currentEntityProperty.GetValue(currentEntity, null),   
        newEntityProperty.GetValue(newEntity, null)
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a generic method to serizlize a class to text (for
How to create a generic method which can call overloaded methods? I tried but
I'd like to create a generic method for converting any System.Enum derived type to
Is it possible to create a generic method with a definition similar to: public
Is it possible to create a generic method with a signature like public static
I am trying to create a generic method that will read an attribute on
I would like to create a method in a base generic class to return
I am trying to create an extension method for the generic delegate Action<T> to
We've created a generic method like so: public TReturnType GetValue(string key) { var configurationParameter
I am trying to create a generic method using EF4 to find the primary

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.