I've got a hierarchy of interfaces that I need passed into a method to return a value given a value path. For instance:
Class.Property. SubProperty.Val ue
I'm using reflection to iterate through the path grabbing the sub-properties down to the "leaf" property at which point the value is returned. The basic concept is this:
Now...even if I replace some of my properties from static classes to interfaces, in C# this will still work as when you implement an interface in C# it must have the same name as the interface, so everything works as you would expect. But if you implement an interface in VB, you can change the name of a property because each property uses the Implements keyword to implement the correct property of the interface. For example:
[Code=vb]Public Property MyIncorrectlyNa medProperty() As String Implements IMyInterface.Ac tualPropertyNam e[/Code]
Whereas in C#, you must use the correct name...
So my question I guess is in order to prevent possible problems with VB programmers calling instance properties by incorrect names, what can I do about this? Other than rapping them on the knuckles with a ruler and telling them not to do that...
Class.Property. SubProperty.Val ue
I'm using reflection to iterate through the path grabbing the sub-properties down to the "leaf" property at which point the value is returned. The basic concept is this:
Code:
Function GetValue(ByVal FreightBill As IFreightBill, ByVal ValuePath As String) As Double
Dim AssemblyName As String = Reflection.Assembly.GetExecutingAssembly().GetName.Name
Dim PathNodes As List(Of String) = ValuePath.Split(".").ToList
If PathNodes(0) <> AssemblyName Then PathNodes.Insert(0, AssemblyName)
Dim CrntInstance As Object = FreightBill
For i As Integer = 2 To PathNodes.Count - 1
Dim CrntType As Type = CrntInstance.GetType()
Dim CrntProperty As String = PathNodes(i)
Dim dynProperty As Reflection.PropertyInfo = CrntType.GetProperty(CrntProperty)
Dim CrntValue = dynProperty.GetValue(CrntInstance, _
System.Reflection.BindingFlags.GetProperty, _
Nothing, _
Nothing, _
Nothing)
CrntInstance = CrntValue
Next
Return CrntInstance
End Function
[Code=vb]Public Property MyIncorrectlyNa medProperty() As String Implements IMyInterface.Ac tualPropertyNam e[/Code]
Whereas in C#, you must use the correct name...
So my question I guess is in order to prevent possible problems with VB programmers calling instance properties by incorrect names, what can I do about this? Other than rapping them on the knuckles with a ruler and telling them not to do that...
Comment