I have been reading up on Reflection and it seems to be a good fit for my application but I have some concerns, mainly around performance. Here is my class below. MyObject may get created 20 times per page request which means CreateInstance will execute only around 20 times per page, max. Question 1: Can I be happy with this in that this does not effect performance in such a significant way?
My main concern is that RunFunction may be called 100's of times per page request. So, Question 2: Is this going to hurt performance and is there a better way of doing this if it does?
My application does know about all the Classes that are named in SomeClassName, but it just doesn't know WHEN to execute the objDynamic functions until runtime.
Question 3: Have their been significant improvements in .net 4.0 for Reflection performance?
Thanks for your time with this.
Class MyObject
dim objDynamic as object = nothing
public sub new(SomeClassName as String)
objDynamic = Activator.CreateInstance(Type.[GetType](SomeClassName))
end sub
Public function RunFunction(strFunctionName As String) As Object
Dim thisType As Type = objDynamic.[GetType]()
Dim theMethod As System.Reflection.MethodInfo = thisType.GetMethod(strFunctionName)
dim objResult as Object = theMethod.Invoke(objDynamic, nothing)
return objResult
End Function
end class
Edit: What if I did this...
Class MyObject
dim objDynamic as object = nothing
Public bolMethodInfo As New List(Of System.Reflection.MethodInfo)
public sub new(SomeClassName as String)
objDynamic = Activator.CreateInstance(Type.[GetType](SomeClassName))
end sub
Public function RunFunction(strFunctionName As String) As Object
Dim thisType As Type = objDynamic.[GetType]()
Dim theMethod As System.Reflection.MethodInfo = Nothing
For Each mi As System.Reflection.MethodInfo In bolMethodInfo
If mi.Name = strFunctionName Then
theMethod = mi
Exit For
End If
Next
If theMethod Is Nothing Then
theMethod = thisType.GetMethod(strFunctionName)
bolMethodInfo.Add(theMethod)
End If
dim objResult as Object = theMethod.Invoke(objDynamic, nothing)
return objResult
End Function
end class