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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:50:39+00:00 2026-06-06T12:50:39+00:00

here is a simple example to clear my intentions. class A { public int

  • 0

here is a simple example to clear my intentions.

class A {
    public int Id
    public string Name
    public string Hash
    public C c
}
class B {
    public int id
    public string name
    public C c
}
class C {
    public string name
}
var a = new A() { Id = 123, Name = "something", Hash = "somehash" };
var b = new B();

I want to set b‘s properties from a. I have tried something but no luck.

public void GenericClassMatcher(object firstModel, object secondModel)
    {
        if (firstModel != null || secondModel != null)
        {
            var firstModelType = firstModel.GetType();
            var secondModelType = secondModel.GetType();
            // to view model
            foreach (PropertyInfo prop in firstModelType.GetProperties())
            {
                var firstModelPropName = prop.Name.ElementAt(0).ToString().ToLower(System.Globalization.CultureInfo.InvariantCulture) + prop.Name.Substring(1); // lowercase first letter
                if (prop.PropertyType.FullName.EndsWith("Model"))
                {
                    GenericClassMatcher(prop, secondModelType.GetProperty(firstModelPropName));
                }
                else
                {
                    var firstModelPropValue = prop.GetValue(firstModel, null);
                    var secondModelProp = secondModelType.GetProperty(firstModelPropName);
                    if (prop.PropertyType.Name == "Guid")
                    {
                        firstModelPropValue = firstModelPropValue.ToString();
                    }
                    secondModelProp.SetValue(secondModel, firstModelPropValue, null);
                }
            }
        }
    }

What shall I do?

  • 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-06T12:50:40+00:00Added an answer on June 6, 2026 at 12:50 pm

    It sounds like you are trying to map one class to another. AutoMapper is the best tool I’ve come across to do this.

    public class A 
        {
            public int Id;
            public string Name;
            public string Hash;
            public C c;
        }
    
        public class B 
        {
            public int id;
            public string name;
            public C c;
        }
    
        public class C 
        {
            public string name;
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
                var a = new A() { Id = 123, Name = "something", Hash = "somehash" };
                var b = new B();
    
                AutoMapper.Mapper.CreateMap<A, B>();
    
                b = AutoMapper.Mapper.Map<A, B>(a);
    
    
                Console.WriteLine(b.id);
                Console.WriteLine(b.name);
    
                Console.ReadLine();
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a simple example of a one-lined IF statement. if(condition) something = blah;
I can't get past this issue I am having. Here's a simple example: class
http://jsfiddle.net/danesoul/tSCus/2/ - Here is working example of simple JQuery UI Tabs interface. Commented string
Very simple example: Model: require 'inventory' class CustomerOrder < ActiveRecord::Base validates_presence_of :name validate :must_have_at_least_one_item,
Simple example: template <class P> class MyT { struct Item { public: Item() {}
This problem is not readily reproducible in a simple example here but was wondering
I am learning and trying simple example using node.js and mongoskin. here is my
I'm studying webrat and cucumber and trying to create simple example. Here is my
Here is example which I have try <?php include 'spider/classes/simple_html_dom.php'; $html = new simple_html_dom();
I have a simple java project (adapted from the example here ), which is

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.