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:

 

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)

How to set a Property Value by Name in C# and VB.NET

To set a Property Value by Name in C# and VB.NET you can use the following snippet.

Sample C#

public static bool SetPropertyByName(this Object obj, string name, Object value)
{
	var prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
	if (null == prop || !prop.CanWrite) return false;
	prop.SetValue(obj, value, null);
	return true;
}

Sample VB.NET

Public Shared Function SetPropertyValueByName(obj As [Object], name As String, value As [Object]) As Boolean
	Dim prop = obj.[GetType]().GetProperty(name, BindingFlags.[Public] Or BindingFlags.Instance)
	If prop Is Nothing OrElse Not prop.CanWrite Then
		Return False
	End If
	prop.SetValue(obj, value, Nothing)
	Return True
End Function

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