Extension Generic methods in .NET (VB.NET)?

Cloning should be something the object itself performs as a regular method, not as an extension method. See the MSDN documentation on MemberwiseClone for an example.

Especially as extension methods cannot access private/protected members. – GCATNM Nov 10 '10 at 11:33 +1 I can't imagine what in the world your cloning code would even look like inside the extension method. – Cody Gray Nov 10 '10 at 11:37 It would be feasible to do a shallow clone with reflection, but that's asking for trouble.

– Snarfblam Nov 10 '10 at 12:13 -1 XML or Binary Serialization can achieve Cloning without explicitly accessing private properties. The question was not about Cloning, but about extension method. – serhio Nov 10 '10 at 12:49.

I've actually done something similar using reflection, to help with modelbinding in an MVC project. I've defined a custom attribute "Updatable", and a generic extension method. This is a bit safer, as I have to explicitly mark attributes to be cloned with my custom attribute (or via a metadata class) for the behaviour to do anything, but I'm sure you could adapt it for your own purposes.

Here's my custom attribute (not very exciting! ) Public Class UpdateableAttribute : Inherits Attribute End Class And here's the extension method Public Sub UpdateObject(Of T)(ByVal targetObject As T, ByVal otherObject As T) Dim metaDataType As MetadataTypeAttribute = GetType(T). GetCustomAttributes(GetType(MetadataTypeAttribute), True).

FirstOrDefault If metaDataType Is Nothing Then For Each prop As Reflection. PropertyInfo In GetType(T).GetProperties() If prop. GetCustomAttributes(GetType(UpdateableAttribute), True).

Count > 0 Then Dim targetValue = prop. GetValue(targetObject, {}) Dim otherValue = prop. GetValue(otherObject, {}) If Not targetValue = otherValue Then prop.

SetValue(targetObject, otherValue, {}) End If End If Next Else Dim targetProps As Reflection.PropertyInfo() = GetType(T).GetProperties() For Each prop As Reflection. PropertyInfo In metaDataType. MetadataClassType.GetProperties() If prop.

GetCustomAttributes(GetType(UpdateableAttribute), True). Count > 0 Then Dim propName As String = prop. Name Dim targetProperty = targetProps.

Single(Function(p) p. Name = propName) Dim targetValue = targetProperty. GetValue(targetObject, {}) Dim otherValue = targetProperty.

GetValue(otherObject, {}) If Not targetValue = otherValue Then targetProperty. SetValue(targetObject, otherValue, {}) End If End If Next End If End Sub Behaves nicely for me, it obviously uses reflection so isn't lightning quick but saves me a heck of a lot of left = right coding. If you have access to the target class, you can just do this, in this example only firstname and surname will clone.

Public Class Person Public Property FirstName As String Public Property Surname As String Public Property ShoeSize As Integer Public Property Gender As Gender Public Property BirthDate As DateTime Public Property FavoriteCarMake End Class And an example using a metadata class to do the same thing, which is good for if you can't edit the target class directly (e.g. Entity Framework classes if you don't fancy editing T4 templates) Public Class Person Public Property FirstName As String Public Property Surname As String Public Property ShoeSize As Integer Public Property Gender As Gender Public Property BirthDate As DateTime Public Property FavoriteCarMake End Class Public Class PersonMetadata Public Property FirstName Public Property Surname End Class In both cases you can simply do something like this: Dim person1 As New Person With ... Dim person2 As New Person With ... person1. UpdateObject(person2).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions