Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将对象绑定到列表?_C#_Object_Generics_Reference_Bind - Fatal编程技术网

C# 如何将对象绑定到列表?

C# 如何将对象绑定到列表?,c#,object,generics,reference,bind,C#,Object,Generics,Reference,Bind,这不是windows窗体应用程序,我不确定使用的术语是否正确。我试图将一个对象绑定到一个列表,这样当对象在列表之外被修改时,这些更改就会反映在列表中。我不完全确定如何开始,我的搜索只会不断地向数据源返回“winform”答案,但这不是我想要的。以下是我到目前为止的情况: 如果您希望测试此代码,可以将其复制到控制台应用程序中。请注意,循环遍历go.Getcomponents()的foreach没有显示名称,因为我认为从列表中拉出时,修改的对象不会仍然被引用。本质上,我试图修改列表外的对象,但当该对

这不是windows窗体应用程序,我不确定使用的术语是否正确。我试图将一个对象绑定到一个列表,这样当对象在列表之外被修改时,这些更改就会反映在列表中。我不完全确定如何开始,我的搜索只会不断地向数据源返回“winform”答案,但这不是我想要的。以下是我到目前为止的情况:

如果您希望测试此代码,可以将其复制到控制台应用程序中。请注意,循环遍历go.Getcomponents()的foreach没有显示名称,因为我认为从列表中拉出时,修改的对象不会仍然被引用。本质上,我试图修改列表外的对象,但当该对象被修改时,列表中的对象也会被修改

重要的是它可以序列化,因为游戏对象将通过网络传输,其中的数据将由服务器读取

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;
    }
}
类测试
{
公共void TestStart()
{
GameObject go=新建GameObject();//创建GameObject
Dog Dog=go.AddComponent();//将狗组件添加到游戏对象
dog.name=“Fluffy”//将狗命名为Fluffy,这应该反映在GameObject的GenericComponent列表中
Dog dog2=go.AddComponent();
dog2.name=“Fuzzy”;
//遍历GameObject go中的所有狗组件,不打印狗名:(
foreach(go.GetComponents()中的Dog dg)
{
控制台写入线(dg.名称);
}
Console.ReadLine();
}
}
[可序列化]
公共类游戏对象
{
List componentList=新列表();
//返回在列表中找到的第一个。
公共GetComponent()
{
返回(T)componentList.Find(c=>c.component.GetType()==typeof(T)).component;
}
//将组件添加到组件列表。
公共添加组件()
{
GenericComponent NewcomComponent=new GenericComponent();/;(T)Activator.CreateInstance(typeof(T));
newComponent.component=(T)Activator.CreateInstance(typeof(T));
组件列表。添加(新组件);
return(T)Activator.CreateInstance(typeof(T));
}
//返回T类型的所有组件
公共列表GetComponents()
{
列表r=新列表();
for(int i=0;i
在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;
}
public添加组件()
{
GenericComponent newcommponent=新的GenericComponent();
newComponent.component=(T)Activator.CreateInstance(typeof(T));
组件列表。添加(新组件);
返回(T)newComponent.component;
}

你似乎混淆了
引用的概念。列表只是引用对象的一种方式,有点像电话簿。当你向列表中添加对象时,你所做的只是将对象的地址放入列表中。因此,你添加的对象和列表引用的对象是一个相同的对象。
AddCom中的代码ponent
对我来说没有多大意义,因为我只希望创建一个类型为
T
的对象……这可能很有趣:一个问题是,您正在创建多个实例,因为
Activator.CreateInstance
被调用了两次。您返回的实例不是您添加到列表中的实例。@MikeCheel-这没有帮助OP as“add component”实际上返回完全分离的副本…此外,如果您有两只狗,请不要将它们命名为“Fluffy”和“Fuzzy”!!哈哈哈,我会将这些名称留给我的祖母小猫们。