How to Map to Protected Property using AutoMapper in C# and VB.NET

To map protected Properties using AutoMapper in C# and VB.NET you can use the following snippet.

Samples

Samples C#

Samples VB.NET

🙁 missing, feel free to provide a VB.NET sample

Another approach would be to create a method in the destination object to do the mapping.

Samples

Sample C#

Sample VB.NET

 

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

 

Please rate this snippet

How to ignore all unmapped properties using Automapper in C# and VB.NET

To ignore all unmapped properties using Automapper in C# and VB.NET you can use the following snippet.

Sample C#

var testClassA = new TestclassA() {Name = "Testname", Title = "Testtitle"};
Mapper.CreateMap<TestclassA, TestclassB>().ForAllMembers(opt => opt.Ignore());
Mapper.CreateMap<TestclassA, TestclassB>().ForMember(source => source.Title, destination => destination.MapFrom(x => x.Title));

var testClassB = Mapper.Map<TestclassA, TestclassB>(testClassA);
Console.WriteLine(testClassB.Title); //prints out "Testtitle"
Console.WriteLine(testClassB.Name); //print nothing  (NULL)

Sample VB.NET

Dim testClassA = New TestclassA() With { _
	Key .Name = "Testname", _
	Key .Title = "Testtitle" _
}
Mapper.CreateMap(Of TestclassA, TestclassB)().ForAllMembers(Function(opt) opt.Ignore())
Mapper.CreateMap(Of TestclassA, TestclassB)().ForMember(Function(source) source.Title, Function(destination) destination.MapFrom(Function(x) x.Title))

Dim testClassB = Mapper.Map(Of TestclassA, TestclassB)(testClassA)
Console.WriteLine(testClassB.Title)
'prints out "Testtitle"
Console.WriteLine(testClassB.Name)
'print nothing  (NULL)

Please rate this snippet

How to make a List or Collection of class read-only in C# and VB.NET

To make a List or Collection of class read-only in C# and VB.NET you can use the following snippet.

Sample C#

public class Test
{
	private readonly List<int> _testList = new List<int>();

	public IList<int> TestList 
	{
		get { return _testList.AsReadOnly(); }
	}

	public Test()
	{
		_testList.Add(0);
		_testList.Add(1);
		_testList.Add(2);
		_testList.Add(3);
		_testList.Add(4);
		_testList.Add(5);
	}
}

Sample VB.NET

Public Class Test
	Private ReadOnly _testList As New List(Of Integer)()

	Public ReadOnly Property TestList() As IList(Of Integer)
		Get
			Return _testList.AsReadOnly()
		End Get
	End Property

	Public Sub New()
		_testList.Add(0)
		_testList.Add(1)
		_testList.Add(2)
		_testList.Add(3)
		_testList.Add(4)
		_testList.Add(5)
	End Sub
End Class

Please rate this snippet

Setting a default value for auto implemented properties

normally you can set a default value for your properties by giving the private property a value.

private string description = "none";
public string Description
{
    get 
    {
        return description;
    }
    set
    {
        description = value;
    }
}

but the auto implemented properties which were introduced with C# 3.0 the syntax does look like this

public string Description { get; set; }

The only way to give auto implemented properties a default value is to set the value in constructor.

Sample:

Constructor for Class with auto implemented propertie with default value

this will give you a default value for your auto-implemented property, without breaking object initializer functionallity.

object initialiser not broken

more informations in the MSDN Auto-Implemented Properties

Please rate this snippet