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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:43:10+00:00 2026-05-23T20:43:10+00:00

I have 2 classes Customer and Person(share exactly the same properties and these properties

  • 0

I have 2 classes Customer and Person(share exactly the same properties and these properties should be filled in by a request

I would like to write a generic method like

    //Usage Customer myCustomer =CreateCustomerOrPerson<Customer>(myRequest)
    //Usage Person myPerson =CreateCustomerOrPerson<Person>(myRequest)
    public static T FillPropertiesOfCustomerOrPerson<T>(Request request)
    {
       //not sure how I would I do it to fill the properties.
      // T a = default(T);

      //a.Name=request.Name;
      //a.Surname=request.Surname;

      // if (a is Customer)
       //{
          //?
      /// }
       return (T)a;
    } 

    How would you write this generic method to avoid having 2 methods (one for customer and one for person)?

Edit

I have no control over these classes. I just need to fill the properties and I was wondering if I could write a generic method rather than 2 specific ones.

  • 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-23T20:43:10+00:00Added an answer on May 23, 2026 at 8:43 pm

    Given the requirements, your options are somewhat limited. If you don’t want to make use of the dynamic keyword (due to .NET version or whatever), you could do this old-style and use reflection. A possible implementation of that follows:

    private const string PROP_NAME = "Name";
    private const string PROP_SURNAME = "Surname";
    
    public static T FillPropertiesOfCustomerOrPerson<T>(Request request)
        where T : new()
    {
        if (typeof(T) != typeof(Person) && typeof(T) != typeof(Customer))
        {
            throw new Exception(
                string.Format("{0} is not a supported type.", typeof(T).Name)
            );
        }
    
        PropertyInfo name = typeof(T).GetProperty(PROP_NAME);
        PropertyInfo surname = typeof(T).GetProperty(PROP_SURNAME);
    
        T t = new T();
    
        name.SetValue(t, request.Name, null);
        surname.SetValue(t, request.Surname, null);
    
        return t;
    }
    

    Optionally, you could remove the where T : new() and replace the instantiation code with something like this:

    T t = (T)Activator.CreateInstance(typeof(T));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mapped several java classes like Customer, Assessment, Rating, ... to a database
I have several classes such as Order, Customer, etc. These classes serve for holding
I have classes that are named exactly the same across different plug-ins that I
I have classes which have automatic properties only like public customerName {get; set;}. They
I have this scenario: One custom class (Customer) with some properties, like this: public
I have classes for entities like Customer, InternalCustomer, ExternalCustomer (with the appropriate inheritance) generated
I've been instructed to create two Classes: Customer and Barber, Barber should have a
I have 3 abstract classes (Customer, Tender, Site). For Customer has inherited class such
I have a project which it's domain contain following classes: Courier Customer Food Order
If I have classes A, B, C, D, E , and interfaces like X,

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.