{"id":52,"date":"2012-02-26T21:54:00","date_gmt":"2012-02-26T21:54:00","guid":{"rendered":"http:\/\/tpodolak.com.hostingasp.pl\/blog\/2012\/02\/26\/definiowanie-wlasnego-operatora-rzutowania\/"},"modified":"2016-01-31T00:19:59","modified_gmt":"2016-01-31T00:19:59","slug":"definiowanie-wlasnego-operatora-rzutowania","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2012\/02\/26\/definiowanie-wlasnego-operatora-rzutowania\/","title":{"rendered":"Definiowanie w\u0142asnego operatora rzutowania"},"content":{"rendered":"<p>Witam<br \/>\nOstatnio modyfikuj\u0105c m\u00f3j kod stan\u0105\u0142em przed problemem rzutowania obiektu typu MembershipUser(z namespaca System.Web.Security) na m\u00f3j w\u0142asny typ UserDTO. Klasa UserDTO nie wyr\u00f3\u017cnia si\u0119 niczym specjalnym i wygl\u0105da nast\u0119puj\u0105co<\/p>\n<pre lang=\"csharp\">\r\npublic class UserDTO : UserBase\r\n{\r\n    public string Id { get; set; }\r\n\r\n    public string Email { get; set; }\r\n\r\n    public string DisplayName { get; set; }\r\n}\r\n<\/pre>\n<p>Podej\u015bciem do\u015b\u0107 oczywistym w takim przypadku jest utworzenie odpowiedniego konstruktora w klasie UserDTO, przyjmuj\u0105cego jako parametr obiekt typu MembershipUser. Jednak\u017ce mo\u017cna r\u00f3wnie\u017c utworzy\u0107 w\u0142asny operator rzutowania, dzi\u0119ki kt\u00f3remu zapis<\/p>\n<pre lang=\"csharp\"> \r\nMembershipUser user = new MembershipUser(\/*jakies parametry*\/);\r\nUserDTO dtoUser = (UserDTO) user;\r\n<\/pre>\n<p>b\u0119dzie poprawny. Wszystko sprowadza si\u0119 do u\u017cycia dw\u00f3ch s\u0142\u00f3w kluczowych:<\/p>\n<ul>\n<li>operator<\/li>\n<li>explicit<\/li>\n<\/ul>\n<p>W klasie UserDTO musimy dopisa\u0107 nast\u0119puj\u0105cy kod<\/p>\n<pre lang=\"csharp\">\r\npublic static explicit operator UserDTO(MembershipUser user)\r\n{\r\n    if (user == null)\r\n        throw new ApplicationException(\"Parametr user nie mo\u017ce by\u0107 null-em\");\r\n\r\n    return new UserDTO {   \r\n                          Id = user.ProviderUserKey is Guid ? ((Guid)user.ProviderUserKey).ToString() : user.ProviderUserKey.ToString(),\r\n                          Name = user.UserName,\r\n                          Email = user.Email,\r\n                       };\r\n}\r\n<\/pre>\n<p>Jak wida\u0107 tworzymy nowy operator konwersji, w kt\u00f3rym po prawej stronie wyra\u017cenia musi sta\u0107 obiekt typu MembershipUser. Od teraz za ka\u017cdym razem gdy b\u0119dziemy chcieli rzutowa\u0107 obiekt MembershipUser na UserDTO b\u0119dziemy mogli pos\u0142u\u017cy\u0107 si\u0119 nast\u0119puj\u0105c\u0105 konstrukcj\u0105.<\/p>\n<pre lang=\"csharp\"> UserDTO dtoUser = (UserDTO) membershipUser;<\/pre>\n<p>><br \/>\nRzutowanie z UserDTO na MembershipUser nie jest dozwolone (przy wykorzystaniu wy\u017cej wymienionego kodu), ale nic nie stoi na przeszkodzie aby zdefiniowa\u0107 drugi operator rzutowania<\/p>\n<pre lang=\"csharp\">\r\npublic static explicit operator MembershipUser(UserDTO userDto)\r\n{\r\n    \/\/tutaj odpowiedni kod\r\n}\r\n<\/pre>\n<p>kt\u00f3ry zapewni nam konwersj\u0119 odwrotn\u0105<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Witam Ostatnio modyfikuj\u0105c m\u00f3j kod stan\u0105\u0142em przed problemem rzutowania obiektu typu MembershipUser(z namespaca System.Web.Security) na m\u00f3j w\u0142asny typ UserDTO. Klasa UserDTO nie wyr\u00f3\u017cnia si\u0119 niczym specjalnym i wygl\u0105da nast\u0119puj\u0105co public class UserDTO : UserBase { public string Id { get; set; } public string Email { get; set; } public string DisplayName { get; set; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,114],"tags":[159,247],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-c","category-explict","tag-c","tag-cast"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":4,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}