2014-11-13 5 views
0

이것은 Windows 양식 앱이 아니기 때문에 올바른 용어를 사용하고 있는지 잘 모르겠습니다. 개체를 목록에 바인딩 할 때 목록 외부에서 개체가 수정되면 해당 변경 내용이 목록에 반영됩니다. 나는 어떻게 시작해야하는지 완전히 모르겠다. 내 검색은 데이터 소스에 대한 "winform"응답을 계속 반환하지만 이것이 내가 원하는 것은 아니다. 여기에 내가 지금까지 가지고있는 것입니다 :개체를 목록에 바인딩하는 방법은 무엇입니까?

테스트하려는 경우이 코드를 콘솔 응용 프로그램에 복사 할 수 있습니다. foreach가 go.Getcomponents()를 통해 반복되는 것은 수정 된 객체가 목록에서 꺼내 질 때 여전히 참조되는 것으로 생각하지 않기 때문에 이름을 표시하지 않습니다. 기본적으로 목록 외부의 개체를 수정하려고하지만 해당 개체를 수정할 때 목록의 개체도 수정됩니다.

GameObject가 네트워크를 통해 전송되고 서버 내에있는 데이터를 서버에서 읽을 수 있기 때문에 직렬화 할 수 있어야합니다.

class Test 
{ 
    public void TestStart() 
    { 
     GameObject go = new GameObject(); //create GameObject 
     Dog dog = go.AddComponent<Dog>(); //Add a Dog component to the GameObject 
     dog.name = "Fluffy"; //name the dog fluffy, this should be reflected in the GenericComponent list of GameObject 
     Dog dog2 = go.AddComponent<Dog>(); 
     dog2.name = "Fuzzy"; 

     //loop through all dog components in GameObject go, doesn't print dog names :(
     foreach (Dog dg in go.GetComponents<Dog>()) 
     { 
      Console.WriteLine(dg.name); 
     } 
     Console.ReadLine(); 
    } 
} 
[Serializable] 
public class GameObject 
{ 
    List<GenericComponent<Object>> componentList = new List<GenericComponent<Object>>(); 

    //returns first found in list. 
    public T GetComponent<T>() 
    { 
     return (T)componentList.Find(c => c.component.GetType() == typeof(T)).component; 
    } 
    //add a component to component list. 
    public T AddComponent<T>() 
    { 
     GenericComponent<Object> newComponent = new GenericComponent<Object>();//;(T)Activator.CreateInstance(typeof(T)); 
     newComponent.component = (T)Activator.CreateInstance(typeof(T)); 
     componentList.Add(newComponent); 
     return (T)Activator.CreateInstance(typeof(T)); 
    } 
    //returns all components of type T 
    public List<T> GetComponents<T>() 
    { 
     List<T> r = new List<T>(); 
     for (int i = 0; i < componentList.Count; i++) 
     { 
      if (componentList[i].component.GetType() == typeof(T)) 
      { 
       r.Add((T)componentList[i].component); 
      } 
     } 
     return r; 
    } 
} 
[Serializable] 
public class GenericComponent<T> where T : new() 
{ 
    public T component; 
    public GenericComponent() 
    { 
     component = new T(); 
    } 
} 
[Serializable] 
public class Dog 
{ 
    public string name = ""; 
    public Dog() { } 
    public Dog(string name) 
    { 
     this.name = name; 
    } 
} 
+0

'Referencing'의 개념을 혼란스럽게하는 것처럼 보입니다. 목록은 개체를 참조하는 방법 일 뿐이며 전화 번호부와 같습니다. 개체를 목록에 추가하면 개체의 주소가 목록에 추가됩니다. 따라서 추가하는 객체와 목록에서 참조하는 객체는 동일합니다. – Aron

+0

'AddComponent'의 코드는'T' 타입의 객체 하나만 생성 될 것으로 기대하기 때문에 나에게별로 의미가 없습니다 ... –

+0

이것은 흥미로울 것입니다 : http://msdn.microsoft.com/en-us /library/ms743695(v=vs.110).aspx –

답변

1

AddComponent 메서드에서 하나의 구성 요소를 추가 한 다음 다른 구성 요소를 반환합니다. 대신, 당신이 추가 한 동일한 것을 돌려 보내십시오 :

public T AddComponent<T>() 
{ 
    GenericComponent<Object> newComponent = new GenericComponent<Object>(); 
    newComponent.component = (T)Activator.CreateInstance(typeof(T)); 
    componentList.Add(newComponent); 
    return (T)newComponent.component; 
} 
+0

그 트릭을 한 것 같아서 고마워. :) – Euthyphro

+0

또한 개가 두 마리있는 경우 '털이'와 '퍼지'라고 이름을 지어주세요! –

+0

하하하 할머니 새끼 고양이 이름을 남겨 두겠습니다. – Euthyphro

관련 문제