How to modify the contents of a method in .NET

145 Views Asked by At

i'd like to know if there is a way in which I can "shadow" a function, the following class is in an assembly reference of my current project, i cannot modify in any way the assembly's content.

Public Class  CoordinateSpace

Public Function FromPixelsY(ByVal y As Single) As Single
    Return (Me.m_originY + ((y * Me.ZoomY) / (Me.m_dpiY / 100.0F)))
End Function

Public Function ToPixelsY(ByVal y As Single) As Single
    Dim num2 As Single = ((y - Me.m_originY) / Me.ZoomY) * (Me.m_dpiY / 100.0F)
    Me.CheckOverflow(num2)
    Return num2
End Function  
End Class

In addition, within the assembly i have many calls in many classes like the following:

Public Class Printer
      public function testPx() as  boolean
       dim c as new CoordinateSpace
       return c.ToPixelsY(18) > 500
      end function
    End Class

For my project I need the above class to return what is in FromPixelsY when a call to ToPixelsY occurs.

Is there a way to do this? If I inherit the class and override or shadow these methods, when testPx calls function ToPixelsY, it would be actually calling CoordinateSpace method and not my new class' method.

This is in VB.net, but any .NET language for the solution is ok. Hope this was clear, thanks!

1

There are 1 best solutions below

2
On
Public Class  MyCoorSpace 
 inherits CoordinateSpace



Public Overrides Function ToPixelsY(ByVal y As Single) As Single
    Return MyBase.FromPixelsY(y)
End Function  

End Class

That was inheritance

Now, decoration in case class is sealed (NotInheritable in VB.NET)

Public Class  MyCoordSpace // here of course would be nice to implement same interface as CoordinateSpace, if any 

    private  _cs as new CoordinateSpace()

Public Function ToPixelsY(ByVal y As Single) As Single
    Return _cs.FromPixelsY(y)
End Function  

End Class