Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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#_Unity3d - Fatal编程技术网

如果统一C#中的陈述为假,但在手表中为真

如果统一C#中的陈述为假,但在手表中为真,c#,unity3d,C#,Unity3d,我目前正在为Unity3D开发一个用户界面编辑器。此时,我有一个字典,其中键和值都表示一个类型。根据键类型,我想向游戏对象添加一个值类型的组件并返回它: protected Dictionary<string, string> eventHandlers = new Dictionary<string, string>(); public virtual EventHandler GetHandler(System.Type type) { string nam

我目前正在为Unity3D开发一个用户界面编辑器。此时,我有一个字典,其中键和值都表示一个类型。根据键类型,我想向游戏对象添加一个值类型的组件并返回它:

protected Dictionary<string, string> eventHandlers = new Dictionary<string, string>();

public virtual EventHandler GetHandler(System.Type type)
{
    string name = type.Name;
    if(eventHandlers.ContainsKey(name))
    {
        System.Type eventHandlerType = TypeUtility.GetType(eventHandlers[name], null);
        return this.gameObject.AddComponent(eventHandlerType) as EventHandler;
    }
    return null;
}
[编辑二] 所以,我找到了我的问题所在

上面的代码驻留在我的类InterfaceElement中:

public abstract class InterfaceElement : MonoBehaviour, ISerializationCallbackReceiver
{
    protected Dictionary<string, string> eventHandlers = new Dictionary<string, string>();
}
公共抽象类接口元素:MonoBehavior,ISerialization CallbackReceiver
{
受保护的Dictionary eventHandlers=新字典();
}
对于我不同的界面元素,比如我的按钮,我想我可以这样覆盖字典:

public class Button : InterfaceElement, IOnClick
{
    protected new Dictionary<string, string> eventHandlers = new Dictionary<string, string>()
    {
        { typeof(IOnClick).Name, typeof(OnClickHandler).Name }
    };
}
公共类按钮:界面元素,单击
{
受保护的新词典事件处理程序=新词典()
{
{typeof(IOnClick).Name,typeof(OnClickHandler).Name}
};
}

显然,C#或Unity不喜欢这样。做某事的正确方法是什么?我不能使用构造函数,因为我的InterfaceElement是单行为的,对吧。事实证明,新技术并没有达到我想象的效果。“新建”不会覆盖字段,而是隐藏基本字段。这意味着基类仍将看到基字段。我的解决方案是不使用字段,而是使用方法返回字典,如下所示:

public abstract class InterfaceElement : MonoBehaviour
{
    protected virtual Dictionary<System.Type, System.Type> GetEventHandlers()
    {
        return null;
    }

    public virtual EventHandler GetHandler(System.Type type)
    {
        Dictionary<System.Type, System.Type> eventHandlers = GetEventHandlers();
        if(eventHandlers != null && eventHandlers.ContainsKey(type))
            return this.gameObject.AddComponent(eventHandlers[type]) as EventHandler;
        return null;
    }
}

public class Button : InterfaceElement, IOnClick
{
    protected override Dictionary<System.Type, System.Type> GetEventHandlers()
    {
        return new Dictionary<System.Type, System.Type>()
        {
            {typeof(IOnClick), typeof(OnClickHandler)}
        };
    }
}
公共抽象类接口元素:MonoBehavior
{
受保护的虚拟字典GetEventHandlers()
{
返回null;
}
公共虚拟事件处理程序GetHandler(System.Type类型)
{
字典eventHandlers=GetEventHandlers();
if(eventHandlers!=null&&eventHandlers.ContainsKey(类型))
将此.gameObject.AddComponent(eventHandlers[type])作为EventHandler返回;
返回null;
}
}
公共类按钮:界面元素,单击
{
受保护的重写字典GetEventHandlers()
{
返回新字典()
{
{typeof(IOnClick),typeof(OnClickHandler)}
};
}
}

感谢您的评论和帮助找到答案

您应该在问题中添加
name
的值和dictionnary@ThomasAyoub我添加了值,您可以尝试
name.GetHashCode()==eventHandlers.ElementAt(0.Key.GetHashCode())
?请告诉我有关
TypeUtility.GetType
方法及其功能的信息。@u请确认没有调用带有TypeUtility.GetType的行。代码返回空值,就好像eventHandlers.ContainsKey(name)返回false一样,尽管在监视中是真的。您是否尝试使用覆盖而不是新建?是的,您不能在字段上使用覆盖。@ScubaKay不是每次调用函数时都重新创建相同的字典,而是可以将字典存储在私有字段中。这是假设您从未更改字典,或者您主动希望共享更改。此外,只有在代码可能重复运行的情况下,这才值得付出努力。
public abstract class InterfaceElement : MonoBehaviour
{
    protected virtual Dictionary<System.Type, System.Type> GetEventHandlers()
    {
        return null;
    }

    public virtual EventHandler GetHandler(System.Type type)
    {
        Dictionary<System.Type, System.Type> eventHandlers = GetEventHandlers();
        if(eventHandlers != null && eventHandlers.ContainsKey(type))
            return this.gameObject.AddComponent(eventHandlers[type]) as EventHandler;
        return null;
    }
}

public class Button : InterfaceElement, IOnClick
{
    protected override Dictionary<System.Type, System.Type> GetEventHandlers()
    {
        return new Dictionary<System.Type, System.Type>()
        {
            {typeof(IOnClick), typeof(OnClickHandler)}
        };
    }
}