Extension Method is not a member of Class

2020-07-05 07:21发布

问题:

For some time now I have been using extension methods to expand the functionality of already existing classes. An example:

<System.Runtime.CompilerServices.Extension()> _
Public Sub DrawRoundedRectangle(graphics As Graphics, pen As Pen, x As Single, y As Single, width As Single, height As Single, radius As Single)
    graphics.DrawRoundedRectangle(pen, x, y, width, height, radius, RectangleEdgeFilter.All)
End Sub

This happily extends the graphics object so that I can call: e.graphics.DrawRoundedRectangle()

However every so often Visual Studio will report:

DrawRoundedRectangle is not a member of System.Drawing.Graphics

This means that I am unable to build due to the errors, and as of yet other than restarting Visual Studio (time consuming) I am yet to find a way to overcome these errors.

I have numerous extension methods (currently around 10) on various different classes, all of which fall over at the same time, this generates 66 errors.

My question is, Is there anything I am missing whilst creating these extension methods, or is there anything that can be done to stop these errors occuring?

回答1:

Do you define the extensions in a Public Module? Tried to 'rebuild solution', does it fix the issue?