Метод расширения называется, как, например метод, но на самом деле является статическим методом. Указатель экземпляра «this» является параметром.
И: Вы должны указать это ключевое слово перед тем, как выбрать соответствующий метод.
public static class ExtensionMethods
{
public static string UppercaseFirstLetter(this string value)
{
// Uppercase the first letter in the string this extension is called on.
if (value.Length > 0)
{
char[] array = value.ToCharArray();
array[0] = char.ToUpper(array[0]);
return new string(array);
}
return value;
}
}
class Program
{
static void Main()
{
// Use the string extension method on this value.
string value = "dot net perls";
value = value.UppercaseFirstLetter(); // Called like an instance method.
Console.WriteLine(value);
}
}
см. http://www.dotnetperls.com/extension для получения дополнительной информации.
** Edit: попробуйте ниже пример раз и снова комментируя
pb.Location=pb.FromCartesian(new Point(20, 20));
, чтобы увидеть результат **
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb = new PictureBox();
pb.Size = new Size(200, 200);
pb.BackColor = Color.Aqua;
pb.Location=pb.FromCartesian(new Point(20, 20));
Controls.Add(pb);
}
}
public static class PictureBoxExtensions
{
public static Point ToCartesian(this PictureBox box, Point p)
{
return new Point(p.X, p.Y - box.Height);
}
public static Point FromCartesian(this PictureBox box, Point p)
{
return new Point(p.X, box.Height - p.Y);
}
}
}
См Edit. Я надеюсь, что это помогает. –