C# AutoMapper Tutorial

C# AutoMapper Tutorial

using System;
using AutoMapper;

public class Foo
{
	public string A { get; set; }
	public int B { get; set; }
}

public class Program
{
	public static void Main()		
	{
		Mapper.CreateMap<Foo,Foo>();
		
		var foo = new Foo { A="test", B=100500 };
		
		var foo_copy = Mapper.Map<Foo>(foo);
		
		Console.WriteLine("foo type is {0}", foo.GetType());
		Console.WriteLine("foo_copy type is {0}", foo_copy.GetType());

		Console.WriteLine("foo.A={0} foo.B={1}", foo.A, foo.B);
		Console.WriteLine("foo_copy.A={0} foo_copy.B={1}", foo_copy.A, foo_copy.B);
	}
}

or map propeties with same name from one type to another

using System;
using AutoMapper;

public class Foo
{
	public string A { get; set; }
	public int B { get; set; }	
}

public class Bar
{
	public string A { get; set; }
	public int B { get; set; }	
}

public class Program
{
	public static void Main()		
	{
		Mapper.CreateMap<Foo,Bar>();
		
		var foo = new Foo { A="test", B=100500 };
		
		var bar = Mapper.Map<Bar>(foo);
		
		Console.WriteLine("foo type is {0}", foo.GetType());
		Console.WriteLine("bar type is {0}", bar.GetType());

		Console.WriteLine("foo.A={0} foo.B={1}", foo.A, foo.B);
		Console.WriteLine("bar.A={0} bar.B={1}", bar.A, bar.B);
	}
}

How to debug external class library projects in visual studio?

Try disabling Just My Code (JMC).

  • Tools -> Options -> Debugger
  • Uncheck "Enable Just my Code"

By default the debugger tries to restrict the view of the world to code that is only contained within your solution. This is really heplful at times but when you want to debug code which is not in your solution (as is your situation) you need to disable JMC in order to see it. Otherwise the code will be treated as external and largely hidden from your view.

EDIT

When you're broken in your code try the following.

  • Debug -> Windows -> Modules
  • Find the DLL for the project you are interested in
  • Right Click -> Load Symbols -> Select the Path to the .PDB for your other project