.net 3.5, VS 2010... this is for an asp.net website.
I have an class called Agency. there is a second class called Agency_Queries. Agency_Queries inhertis the Agency class. I'm trying to create a function that will copy the like properties in Agency to Agency_Queries. I figured out how to do that.. but when i try to make it more generic so that i can pass in my class name and lists i'm doing something wrong.
So if there is an list(of Agency) that needs to be copied to list(of Agency_Queries) i've got something like the following.
Dim AgencyS As List(Of Agency) = Nothing
Dim Oc As New Agency_Controller
AgencyS = Oc.GetAgencyData(0)
Dim AgencyQueriesS As New List(Of Agency_Queries)
Dim _itemProperties() As Reflection.PropertyInfo = AgencyS.Item(0).GetType.GetProperties()
For Each item In AgencyS
Dim X As NewAgency_Queries
_itemProperties = item.GetType().GetProperties()
For Each p As Reflection.PropertyInfo In _itemProperties
For Each s As Reflection.PropertyInfo In X.GetType().GetProperties()
If p.Name = s.Name Then
s.SetValue(X, p.GetValue(item, Nothing), Nothing)
End If
Next
Next
AgencyQueriesS.Add(X)
Next
the problem is when i go to make this generic by the Dim X as new Agency_Queries. How do i go about creating a new instance of the class in a generic sense. I need to have it be a new instance or each time it gets added to teh AgencyQueriesS list all the objects have the same data values.
Here is the generic version... not working
Shared Function CopyObject(ByVal inputList As Object, ByVal OutputClass As Object, ByVal outputList As Object) As Object
Dim _itemProperties() As Reflection.PropertyInfo = inputList.Item(0).GetType.GetProperties()
For Each item In inputList
Dim oClean As Object = OutputClass
For Each p As Reflection.PropertyInfo In _itemProperties
For Each s As Reflection.PropertyInfo In oClean.GetType().GetProperties()
If p.Name = s.Name Then
s.SetValue(oClean, p.GetValue(item, Nothing), Nothing)
End If
Next
Next
outputList.Add(oClean)
Next
Return outputList
End Function
thanks shannon
I took a little of this and that and came up with this:
I use the following: