한 줄 요약
확장 메서드 : 'static' 클래스의 'static' 메서드 이어야하며, 특정 매개변수의 타입에 대해 자주 발생하는 간단한 변형, 타입 변환 등에 사용. 생성자 없이 사용할 수 있으므로 여러 클래스에서 자주 발생하는 경우에 적용하기에 유리
public static class ExtensionMethods
{
public static [반환 타입] [메서드 이름](this [매개 변수])
{
...
}
}
[매개 변수타입] testInstance
testInstance.[메서드 이름]();
public static class ExtensionMethods
{
public static Vector3 ToVector3(this string str)
{
if (str[0] == '(' && str.Last() == ')')
{
var pos = str.Substring(1, str.Length - 2).Split(',');
return new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
}
return Vector3.zero;
}
public static Vector2 ToVector2(this string str)
{
if (str[0] == '(' && str.Last() == ')')
{
var pos = str.Substring(1, str.Length - 2).Split(',');
return new Vector2(float.Parse(pos[0]), float.Parse(pos[1]));
}
return Vector2.zero;
}
}