// Define an interface named IMyInterface.
namespace DefineIMyInterface
{
using System;
public interface IMyInterface
{
// Any class that implements IMyInterface must define a method
// that matches the following signature.
void MethodB();
}
}
// Define extension methods for IMyInterface.
namespace Extensions
{
using System;
using DefineIMyInterface;
// The following extension methods can be accessed by instances of any
// class that implements IMyInterface.
//확장 메소드를 만들기 위해선 확장 메소드가 포함되는 클래스에 static 을 붙여줘야 한다
public static class Extension
{
//각각 IMyInterface 에 대한 확장 메소드 MethodA 에 대하여 정의한다
//확장 메소드의 경우 this 가 붙는데 this 다음에 넘겨줄 타입을 정해줘야한다
//확장 메소드 또한 오버로드가 가능하다
public static void MethodA(this IMyInterface myInterface, int i)
{
Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, int i)");
}
public static void MethodA(this IMyInterface myInterface, string s)
{
Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, string s)");
}
//이 함수는 호출되지 않는데 왜냐면 각 A,B,C 3개 클래스는 시그니처에 매칭되는 MethodB를 구현하기 때문이다
// This method is never called in ExtensionMethodsDemo1, because each
// of the three classes A, B, and C implements a method named MethodB
// that has a matching signature.
public static void MethodB(this IMyInterface myInterface)
{
Console.WriteLine("Extension.MethodB(this IMyInterface myInterface)");
}
}
}
// Define three classes that implement IMyInterface, and then use them to test
// the extension methods.
namespace ExtensionMethodsDemo1
{
using System;
using Extensions;
using DefineIMyInterface;
//각 클래스는 확장 메소드에 의해 확장된 IMyInterface 인터페이스를 상속 받는다
class A : IMyInterface
{
public void MethodB() { Console.WriteLine("A.MethodB()"); } //MethodB 구현 정의함
}
class B : IMyInterface
{
public void MethodB() { Console.WriteLine("B.MethodB()"); } //MethodB 구현 정의함
public void MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); }
}
class C : IMyInterface
{
public void MethodB() { Console.WriteLine("C.MethodB()"); } //MethodB 구현 정의함
public void MethodA(object obj)
{
Console.WriteLine("C.MethodA(object obj)");
}
}
public class DemoClass
{
static void Main(string[] args)
{
// Declare an instance of class A, class B, and class C.
// For a, b, and c, call the following methods:
// -- MethodA with an int argument
// -- MethodA with a string argument
// -- MethodB with no argument.
//
A a = new A();
B b = new B();
C c = new C();
// A contains no MethodA, so each call to MethodA resolves to
// the extension method that has a matching signature.
a.MethodA(1); // Extension.MethodA(IMyInterface, int)
a.MethodA("hello"); // Extension.MethodA(IMyInterface, string)
// A has a method that matches the signature of the following call
// to MethodB.
a.MethodB(); // A.MethodB()
Console.WriteLine("");
// B has methods that match the signatures of the following
// method calls.
b.MethodA(1); // B.MethodA(int)
b.MethodB(); // B.MethodB()
// B has no matching method for the following call, but
// class Extension does.
b.MethodA("hello"); // Extension.MethodA(IMyInterface, string)
Console.WriteLine("");
//C의 경우에는 확장 메소드MethodA가 있어도 C클래스에서 object 에 대한 함수를 정의해놨기 때문에
//클래스(C)의 MethodA 가 호출되게 된다
// C contains an instance method that matches each of the following
// method calls.
c.MethodA(1); // C.MethodA(object)
c.MethodA("hello"); // C.MethodA(object)
c.MethodB(); // C.MethodB()
}
}
}