본문 바로가기

C#

(30)
Deep Copy 밑에 적어놓은 건 교수님 코드 예외 사항 많아서 그대로 쓰진 못하고 코드 연구할 때에만 쓰기! public static T DeepCopy(this T obj) where T : new() { Type type = obj.GetType(); if(type.IsClass) { T.clone = new T(); FieldInfo[] fields = type.GetFields(); foreach(FieldInfo field in fields) { field.SetValue(clone, (field.GetValue(obj)).DeepCopy()); } return (T)clone; } return (T)obj; } } 단점: 클래스가 멤버를 자기 자신으로 갖고 있을 때 무한반복해버림
Parameter / Arguments 차이 void Test(int a, int b) // Parameter { } void Start() { Test(50,40) //Argument }
리플렉션 런타임 시점에 처리 가능. 컴파일 시점에 처리되는 것들 런타임 시점엔 모르니까.. 런타임 시점에서 리플렉션 써서 어거지로 갖고 오는거 나중에 이런게 생길거니까 미리 세팅 해놓으라고 위임하는 느낌인듯 using System.Reflection 추가 필수 type.GetFields 멤버 변수 가져오기 type.GetPropertys 멤버 프로퍼티 가져오기 MethodInfo[] methods = type.GetMethods(); //멤버 함수 가져옴 GetComponent("이름") 문자열은 바꾸기 쉬우니까.. ex) Debug.Log(type.GetField("tempValue"), GetValue(tempA)); 누구한테서 어떤 값을 가져올 것이냐. Type type = tempA.GetType(); t..
template method ... using System; public abstract class Dragon { public int hp; public int atk; public abstract void UpGround(); public abstract void DownGround(); public bool IsCheackTargetAlive() { return true; } public void Attack() { UpGround(); Debug.Log("공격한다"); DownGround(); if(IsCheackTargetAlive()) { Attack(); } } } public class BlueDragon : Dragon { public override void UpGround() { Debug.Log("올라왔다");..
EventHandler public event EventHandler 이벤트핸들러이름; 규격: public delegate void EventHandler(object sender, EventArgs e); sender: 호출한 애. 일반적으론 자기 자신. e: 추가적으로 전달해야할 부분이 있을 때 씀. 이벤트에 대한 매개 변수 호출 시 onDieHandler(this, null); 이런 식 *this -> object 형태로 보내주게 됨 묶을 때 atk +=((Monster)sender).atk; 이런 식으로 Monster로 만들어줘야. 이벤트가 발생하면 이벤트핸들러가 그걸 받아 이벤트핸들러에게 구독되어있는 이벤트 리스너들에게 알려줌. 그럼 리스너들이 그걸 받아 처리할거 처리함. 상황을 컨트롤하는 역할. public dele..
프로퍼티 get 접근자에 대한 코드 블록은 속성을 읽을 때(호출 시) 실행 set 접근자에 대한 코드 블록은 속성에 새로운 값을 할당시킬 때 실행 class 클래스명 { 데이터타입 필드명; 접근한정자 데이터타입 프로퍼티명 { get{ return 필드명; } set{ 필드명 = value; } } }
전략적 패턴 다양한 객체에 접근이 필요할 때 공통 분모를 추상화하여 인터페이스 클래스를 만들어 이를 접근점으로 활용 public Interface I이름 { void function(); } using System.Collections; using System.Collections.Generic; using UnityEngine; public interface IWeapon{ void Shoot(GameObject obj); } using System.Collections; using System.Collections.Generic; using UnityEngine; public class Arrow : MonoBehaviour, IWeapon { void Start () { } public void Shoot(Ga..
람다 이름이 없는 메서드. 즉석에서 구현할 수 있음 대충 한 줄 짜리 함수 만들기 아까울 때 쓰면 될 듯 형식 ( ) => { }; (매개변수들..) => { 리턴 타입에 맞는 값 } ; 예시 onAction += () => Debug.Log("처리A"); 오버라이딩 가능성 있을 경우 X.. 3줄 이상 돼도 X.. 가독성 떨어짐 예시 private int health = 0; public void RestoreHealth(int amount) { health += amount; } public bool IsDead() { return (health health += amount; public bool IsDead() => (health health = value; } public bool IsDead => (..