To check if a Object has an Attribute in Python you can use the snippet below.
Sample Python
if hasattr(myObject, 'myAttributeName'): # myObject.myAttributeName exists.
To check if a Object has an Attribute in Python you can use the snippet below.
if hasattr(myObject, 'myAttributeName'): # myObject.myAttributeName exists.
To map protected Properties using AutoMapper in C# and VB.NET you can use the following snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System; using AutoMapper; namespace de.Fesslersoft.AutoMapperMapToProtectedProperties { class Program { static void Main(string[] args) { Product prod = new Product {Number = "Test", Price = 100, Quantity = 50}; Mapper.Initialize(cfg => { //without this line the ItemQuantity will be 10 and the Price 5 //with this line the ItemQuantity will be 50 and the Price 100 cfg.ShouldMapProperty = p => p.GetGetMethod(true).IsPrivate || p.GetGetMethod(true).IsHideBySig; Mapper.CreateMap<Product, Item>(); }); Item item = Mapper.Map<Product, Item>(prod); Console.WriteLine("ItemNumber: {0}; ItemQuantity: {1}, ItemPrice: {2}", item.Number, item.GetQuantity(), item.GetPrice()); Console.Read(); } public class Product { public string Number { get; set; } public short Quantity { get; set; } public decimal Price { get; set; } } public class Item { public Item() { Quantity = 10; Price = 5; } public string Number { get; set; } protected short Quantity { get; set; } private decimal Price { get; } public decimal GetPrice() { return Price; } public short GetQuantity() { return Quantity; } } } } |
🙁 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.
1 |
Mapper.CreateMap<Source, Destination>().AfterMap((src, dest) => dest.MethodToSetProtectedProperty(Source.SourceProperty)); |
1 |
Mapper.CreateMap(Of Source, Destination)().AfterMap(Function(src, dest) dest.MethodToSetProtectedProperty(Source.SourceProperty)) |
If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.
Related links:
To ignore all unmapped properties using Automapper in C# and VB.NET you can use the following snippet.
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)
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)
To make a List or Collection of class read-only in C# and VB.NET you can use the following snippet.
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); } }
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
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:
this will give you a default value for your auto-implemented property, without breaking object initializer functionallity.
more informations in the MSDN Auto-Implemented Properties